我有一个实体代码第一个模型,实体Students
和Courses
具有多对多关系。这是我的实体:
课程:
public class Course
{
public int CourseId { get; set; }
public string Name { get; set; }
public ICollection<Student> Students { get; set; }
public Course()
{
Students = new HashSet<Student>();
}
}
学生:
public class Student
{
public int StudentId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public ICollection<Course> Courses { get; set; }
public Student()
{
Courses = new HashSet<Course>();
}
}
这是我的DbContext:
public class UniversitySystemContext : DbContext
{
public UniversitySystemContext()
: base("UniversityDb")
{
}
public DbSet<Course> Courses { get; set; }
public DbSet<Student> Students { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Course>()
.HasMany(c => c.Students)
.WithMany(p => p.Courses).
Map(
m =>
{
m.MapLeftKey("CoursesId");
m.MapRightKey("StudentId");
m.ToTable("StudentCourses");
});
}
}
当我尝试插入新课程时,我得到了这样的信息:&#34;为表格&#39;课程&#39;指定了多个标识列。每个表只允许一个标识列。&#34;
那么我错过了什么以及如何解决?