Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 具有其他属性的关系映射表?_Entity Framework_Entity Framework 4.1_Ef Code First_Code First_Entity Relationship - Fatal编程技术网

Entity framework 具有其他属性的关系映射表?

Entity framework 具有其他属性的关系映射表?,entity-framework,entity-framework-4.1,ef-code-first,code-first,entity-relationship,Entity Framework,Entity Framework 4.1,Ef Code First,Code First,Entity Relationship,我有三门课: public partial class Student : Contact { //Inherited from Contact: //public int ContactId { get; set; } //public string FirstName { get; set; } //public string LastName { get; set; } public virtual StudentExam StudentEx

我有三门课:

public partial class Student : Contact
{   
    //Inherited from Contact:
    //public int ContactId { get; set; }
    //public string FirstName { get; set; }
    //public string LastName { get; set; }

    public virtual StudentExam StudentExam { get; set; }
}

public partial class Exam
{
    public int ExamId { get; set; }
    public string Title { get; set; }
    public DateTime Date { get; set; }

    public virtual StudentExam StudentExam { get; set; }
}

public partial class StudentExam
{
    public byte Score { get; set; }
    public int ContactId { get; set; }
    public int ExamId { get; set; }

    public virtual Student Student { get; set; }
    public virtual Exam Exam { get; set; }
}
当尝试初始化DbContext时,它抛出一个
modelvalidateException
: 在模型生成过程中检测到一个或多个验证错误: \tSystem.Data.Entity.Edm.EdmEntityType::EntityType“StudentExam”未定义键。定义此EntityType的键。 \tSystem.Data.Entity.Edm.EdmEntitySet:EntityType:EntitySet“StudentExam”基于未定义键的类型“StudentExam”

我尝试将
StudentExam
class”属性更改为以下内容:

[Key, ForeignKey("Student"), Column(Order = 0)]
public int ContactId { get; set; }
[Key, ForeignKey("Exam"), Column(Order = 1)]
public int ExamId { get; set; }
现在我得到了一个例外:

\tSystem.Data.Entity.Edm.edmsociationEnd::多重性在关系“StudentExam_Student”中的角色“StudentExam_Student_Source”中无效。因为依赖角色属性不是键属性,所以依赖角色的多重性上限必须为“*”。 \tSystem.Data.Entity.Edm.edmsociationEnd::多重性在关系“StudentExam\u考试”中的角色“StudentExam\u考试源”中无效。因为依赖角色属性不是键属性,所以依赖角色的多重性上限必须为“*”


有没有办法通过数据注释来实现这一点(当我可以使用数据批注时,我不喜欢使用fluent API;fluent API会导致代码混乱。

这不是关于数据批注或fluent API,而是关于定义不正确的类-在类中定义的关系根本无法映射,因为它们在关系级别无效。您必须修改类:

public partial class Student : Contact
{   
    public virtual ICollection<StudentExam> StudentExams { get; set; }
}

public partial class Exam
{
    ...

    public virtual ICollection<StudentExam> StudentExams { get; set; }
}
公共部分班级学生:联系
{   
公共虚拟ICollection StudentExams{get;set;}
}
公开部分班级考试
{
...
公共虚拟ICollection StudentExams{get;set;}
}
一旦定义了这种关系,您就可以使用数据注释在
StudentExam
类中定义键,它将起作用


顺便说一句,fluent api不会导致混乱的代码。混乱的代码是由程序员创建的,而不是由api创建的。数据注释反过来违反了POCO原则。

我保持POCO的整洁,以后再向其添加元数据。添加元数据的好处也是独立验证(这也可以通过fluent实现吗?).总之,我所说的凌乱代码是指所有流畅的代码都放在一个
OnModelCreated
方法中,而不是放在每个单独的实体上(通过这种方法或其他方法的实现)。我处理的是生成的实体,所以它们无论如何都不是真正的开放边缘POCO(它使用构造函数初始化nav.properties,因此无法在分部类中写入一个ctor。Sln是我添加的一个
OnCreated
分部方法,并在gen.entity中调用的。