此摘录代码使用显式Junction表成功创建 many-many 关系,其中包含其他数据 .
问题: I want to be able to access Courses from Student and vice versa,
(因此注释了 virtual 属性 . 但如果我取消注释它,它会导致错误(见下文))
如果我没有显式创建联结表(没有其他数据),则虚拟关键字可以正常工作,因为EF会按惯例创建联结表 .
题:
如何在不通过注册的情况下让学生访问课程?或者那是不可能的?如果不可能,那么最好的方法是什么呢?
(EF和C#的初学者)
public class Student
{
[Key]
public int StudentId { get; set; }
public string StudentName { get; set; }
//public virtual Course Courses { get; set; }
}
public class Course
{
[Key]
public int CourseId { get; set; }
public string CourseName { get; set; }
//public virtual Student Students { get; set; }
}
public class Enrollment
{
[Key]
public int EnrollmentId { get; set; }
public Student Student { get; set; }
public Course Course { get; set; }
public string Grade { get; set; }
}
public class ManyMany : DbContext, IContext
{
public DbSet<Student> Students { get; set; }
public DbSet<Course> Courses { get; set; }
public DbSet<Enrollment> Enrollments { get; set; }
public void Run()
{
Database.SetInitializer(new DropCreateDatabaseAlways<ManyMany1>());
this.Courses.Add(new Course() {CourseName = "English"});
this.SaveChanges();
}
}
当我解除公共虚拟......
错误:"Unable to determine the principal end of an association between the types 'EF.Course' and 'EF.Student'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations."
2 回答
关于这个问题的老问题,答案和更多信息:Entity Framework CodeFirst many to many relationship with additional information .
编辑:用法示例:
实体框架可以't automatically determine '多对多'关系,因为它们是在SQL的其他表的帮助下表达的(在您的情况下是
Enrollment
表) . 您可以直接在OnModelCreating
方法中指定映射:另外,请注意,如果实体有许多其他实体,请使用集合进行关系: