我已阅读this great Q&A并尝试制作类似内容。我的模型类是:
public class Person
{
public int PersonId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public ICollection<Course> CoursesAttending { get; set; }
public Person()
{
this.CoursesAttending = new List<Course>();
}
}
public class Course
{
public int CourseId { get; set; }
public string Title { get; set; }
public ICollection<Person> Students { get; set; }
}
public class PersonCourse
{
[Key, Column(Order = 0)]
public int PersonID { get; set; }
[Key, Column(Order = 1)]
public int CourseID { get; set; }
public virtual Person Student { get; set; }
public virtual Course StudentCourse { get; set; }
public int Mark { get; set; }
public string Comment { get; set; }
}
public class SchoolContext : DbContext
{
public DbSet<Course> Courses { get; set; }
public DbSet<Person> People { get; set; }
public DbSet<PersonCourse> PersonCourseLinks { get; set; }
public SchoolContext()
: base("ManyToManyTest")
{
}
}
现在,我尝试添加一个新人,并在他的课程列表中添加一个新课程:
[HttpPost]
public ActionResult Create(Person person)
{
if (ModelState.IsValid)
{
Course studentCourse;
try
{
studentCourse = db.Courses.ToList<Course>().First();
}
catch
{
studentCourse = new Course() { Title = "HTML" };
}
person.CoursesAttending.Add(studentCourse);
db.People.Add(person);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(person);
}
一切顺利,但是当我打开创建的数据库时,我看到Person和Course类有2个链接表 - PersonCourses(带字段:PersonID,CourseID,Mark,Comment)和PersonCourse1(带字段:PersonID, CourseID),只有PersonCourse1有行(实际上是一行)。为什么会这样?我做的事情不正确吗?我希望只能看到一个链接表 - PersonCourses表...
答案 0 :(得分:1)
我希望只能看到一个链接表 - PersonCourses表
然后,您必须通过PersonCourse
导航属性将Person
实体与CoursesAttending
实体相关联。必须对Course
实体做同样的事情。
public class Person
{
public int PersonId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public ICollection<PersonCourse> CoursesAttending { get; set; }
public Person()
{
this.CoursesAttending = new List<PersonCourse>();
}
}
public class Course
{
public int CourseId { get; set; }
public string Title { get; set; }
public ICollection<PersonCourse> Students { get; set; }
}
答案 1 :(得分:0)
将外键关联提供给gerund表中的外键属性:
public class PersonCourse
{
[Key, Column(Order = 0)]
public int PersonID { get; set; }
[Key, Column(Order = 1)]
public int CourseID { get; set; }
[ForeignKey("PersonID")]
public virtual Person Student { get; set; }
[ForeignKey("CourseID")]
public virtual Course StudentCourse { get; set; }
public int Mark { get; set; }
public string Comment { get; set; }
}
答案 2 :(得分:0)
这就是为什么你的代码没有按预期工作的原因:
public class PersonCourse
{
[Key, Column(Order = 0)]
public int PersonID { get; set; }
[Key, Column(Order = 1)]
public int CourseID { get; set; }
public virtual Person Person { get; set; } // Follow built in conventions
public virtual Course Course { get; set; }
public int Mark { get; set; }
public string Comment { get; set; }
}
现在遵循内置的实体框架约定
public int [ClassName][Id] or [Id] //Database Foreign Key column
and
public NavigationProperty [ClassName] // Navigation Property