问题描述
我有三节课:
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时,它抛出ModelValidationException
:在模型生成过程中检测到一个或多个验证错误: \ tSystem.Data.Entity.Edm.EdmEntityType::EntityType'StudentExam'没有定义键.定义此EntityType的键.\ tSystem.Data.Entity.Edm.EdmEntitySet:EntityType:EntitySet'StudentExams'基于未定义键的'StudentExam'类型.
When trying to initialize the DbContext, it throws a ModelValidationException
:One or more validation errors were detected during model generation:\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'StudentExam' has no key defined. Define the key for this EntityType.\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'StudentExams' is based on type 'StudentExam' that has no keys defined.
我尝试将StudentExam
类的属性更改为以下内容:
I tried changing the StudentExam
class' properties to the following:
[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.EdmAssociationEnd::多重性在关系'StudentExam_Student'中的角色'StudentExam_Student_Source'中无效.因为从属角色属性不是关键属性,所以从属角色多重性的上限必须为'*'.\ tSystem.Data.Entity.Edm.EdmAssociationEnd::多重性在关系'StudentExam_Exam'中的角色'StudentExam_Exam_Source'中无效.由于从属角色属性不是关键属性,因此从属角色多重性的上限必须为'*'.
\tSystem.Data.Entity.Edm.EdmAssociationEnd: : Multiplicity is not valid in Role 'StudentExam_Student_Source' in relationship 'StudentExam_Student'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.\tSystem.Data.Entity.Edm.EdmAssociationEnd: : Multiplicity is not valid in Role 'StudentExam_Exam_Source' in relationship 'StudentExam_Exam'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.
有什么方法可以通过数据注释来实现这一点(我不喜欢在可以使用数据注释时使用流利的API;流利的API会导致代码混乱.
Is there any way to achieve this in with data annotations (I don't like using the fluent API when I can use data annotations; The fluent API leads to messy code.
推荐答案
这与数据注释或流畅的api无关,而是与定义不正确的类有关-您的类中定义的关系根本无法映射,因为它们在关系级别上无效.您必须修改您的课程:
It is not about data annotations or fluent api but about incorrectly defined classes - relations defined in your classes cannot be mapped at all because they are not valid at relational level. You must modify your classes:
public partial class Student : Contact
{
public virtual ICollection<StudentExam> StudentExams { get; set; }
}
public partial class Exam
{
...
public virtual ICollection<StudentExam> StudentExams { get; set; }
}
一旦定义了这种关系,就可以使用数据注释在StudentExam
类中定义键,它将起作用.
Once you have this relations defined you can use your data annotations for defining keys in StudentExam
class and it will work.
顺便说一句.流利的api不会导致凌乱的代码.混乱的代码是由程序员而不是由API创建的.数据注释又违反了POCO原则.
Btw. fluent api doesn't lead to messy code. Messy code is created by programmer, not by API. Data annotations in turn violates POCO principle.
这篇关于具有其他属性的关系映射表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!