[英]Entity Framework Code First One Property of Class and List of The Same Class
I'm using entity framework code first approach 我正在使用实体框架代码优先方法
I have a class 我有一堂课
public class Movie
{
public int Id { get; set; }
public string Title { get; set; }
public Person Director { get; set; }
public virtual ICollection<Person> Actors { get; set; }
}
and a class 和一堂课
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
}
When the database is created I get one table Movies
with Id
, Title
, Director_Id
and a table Person
with Id
and Name
. 创建数据库后,我得到一个具有Id
, Title
, Director_Id
表Movies
和具有Id
和Name
的表Person
。 I expect to have a table Movies_Persons
with columns Movie_Id
and Actor_Id
我希望有一个表Movies_Persons
其中包含Movie_Id
和Actor_Id
列
How can I achieve this? 我该如何实现?
Your Problem is, that you don`t tell the Person Class, that there can be multiple Movies per person. 您的问题是,您没有告诉人类,每个人可能有多部电影。
So by adding the following line in your person class: 因此,在您的人员类中添加以下行:
public virtual ICollection<Movie> Movies { get; set; }
Your entity knows that both your classes can have multiple references to the other class. 您的实体知道两个类都可以对另一个类有多个引用。 To fulfill this requirement Entity Framework will create a third table with Movie_ID
and Person_ID
. 为了满足此要求,实体框架将使用Movie_ID
和Person_ID
创建第三个表。
If you want more informations just look for: Entity Framework - Many to many relationship 如果您需要更多信息,请寻找:实体框架-多对多关系
or follow this link: http://www.entityframeworktutorial.net/code-first/configure-many-to-many-relationship-in-code-first.aspx 或点击以下链接: http : //www.entityframeworktutorial.net/code-first/configure-many-to-many-relationship-in-code-first.aspx
You can check out the other articels on that page too, if you are new to entity framework. 如果您是实体框架的新手,也可以在该页面上查看其他文章。
UPDATE: Sorry i missed, that you are already have another reference to your person table. 更新:对不起,我错过了,您已经有另一个引用到您的人员表。 Here you have to tell your entity framework, which way you want to reference the two tables by fluent api. 在这里,您必须告诉您的实体框架,您想通过fluent api引用哪种表。 Check out this stackoverflow answer . 看看这个stackoverflow答案 。 That should do the trick. 这应该够了吧。 You have to insert this code into your OnModelCreating Function of your DbContext Class. 您必须将此代码插入到DbContext类的OnModelCreating函数中。
So your final code should look like this: 因此,您的最终代码应如下所示:
public class Movie
{
public int Id { get; set; }
public string Title { get; set; }
public virtual Person Director { get; set; }
public virtual ICollection<Person> Actors { get; set; }
}
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Movie> Movies_Actors { get; set; }
public virtual ICollection<Movie> Movies_Directors { get; set; }
}
And in your OnModelCreating add following code: 并在您的OnModelCreating中添加以下代码:
modelBuilder.Entity<Movie>()
.HasMany(a => a.Actors)
.WithMany(a => a.Movies_Actors)
.Map(x =>
{
x.MapLeftKey("Movie_ID");
x.MapRightKey("Person_ID");
x.ToTable("Movie_Actor");
});
modelBuilder.Entity<Movie>()
.HasRequired<Person>(s => s.Director)
.WithMany(s => s.Movies_Directors);
I don't have the possibility to test the code, but that should do the trick. 我没有测试代码的可能性,但这应该可以解决问题。 If you have to do some adjustments to make it work, plz add them in the comments, so other ppl can benefit from it. 如果必须进行一些调整才能使其正常运行,请plz将其添加到注释中,以便其他ppl可以从中受益。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.