如何指定从实体X到同一目标实体Y的两个导航属性?
考虑一下我有一个Instructor
类:
public class Instructor { public InstructorTypesEnum Type { get; set; } public virtual ICollection Courses { get; set; } public virtual ICollection CoInstructingCourses { get; set; } }
这里, InstructorTypesEnum
有两个值:讲师和辅导员。 所以两个导航属性Courses
和CoInstructingCourses
应该返回那些不同的课程。
当然,我也很难在Course
类中指定相应的属性
public Guid InstructorId { get; set; } public virtual Instructor Instructor { get; set; } public virtual ICollection Coinstructors { get; set; }
这样做的正确方法是什么?
您可以使用InversePropertyAttribute
在你的模型上,我认为它会(未经validation):
public class Instructor { public InstructorTypesEnum Type { get; set; } [InverseProperty("Instructors")] public virtual ICollection Courses { get; set; } [InverseProperty("Coinstructors")] public virtual ICollection CoInstructingCourses { get; set; } }
更重要的是 – 您不应该使用GUID作为实体ID。 它会伤害性能。
public Guid InstructorId { get; set; }
尝试用例如int替换它。