0

我有两个表:(1)Student 和 (2) Course,我还有一个名为 (3)StudentCourse 的连接表。相关数据:

public class Student
{
  private ICollection<Course> _courses 
  public int Id {get;set;}

  public Student(){
   _courses = new Collection<Course>();
  }

  public ICollection<Course> Courses{
   get {return _courses;}
   set { _courses = value;}
  }
}

public class Course
{
  private ICollection<Student> _students 
  public int Id {get;set;}

  public Course(){
   _students = new Collection<Student>();
  }

  public ICollection<Student> Students{
   get {return _students;}
   set { _students = value;}
  }
}

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Course>()
            .HasMany<Student>(r => r.Students)
            .WithMany(u => u.Courses)
            .Map(m =>
            {
                m.ToTable("StudentCourse");
                m.MapLeftKey("CourseId");
                m.MapRightKey("StudentId");
            });
    }

我的上下文构造函数有: Configuration.LazyLoadingEnabled = false;

当我通过迁移播种我的课程和学生表时,StudentCourse 表没有填充。

我错过了什么?

4

1 回答 1

0

我的错,我未能将 CourseCollection 添加到 Student 类

于 2013-09-12T18:33:49.623 回答