简体   繁体   English

实体框架代码类的第一个属性和相同类的列表

[英]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 . 创建数据库后,我得到一个具有IdTitleDirector_IdMovies和具有IdName的表Person I expect to have a table Movies_Persons with columns Movie_Id and Actor_Id 我希望有一个表Movies_Persons其中包含Movie_IdActor_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_IDPerson_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.

相关问题 实体框架代码优先-派生类与基类不在同一程序集中 - Entity Framework code first - Derived class not in the same assembly as the base class 忽略Entity Framework 4.1 Code First中的类属性 - Ignoring a class property in Entity Framework 4.1 Code First 实体框架代码与同一类的第一个双重关系 - Entity Framework Code First double relation to same class 使用实体框架代码优先映射相同 class 的子项 - Mapping child items of same class with Entity Framework Code First 实体框架5,MVC 4,代码优先-保留列表 <string> 在课堂上 - Entity Framework 5, MVC 4, Code First - Persisting a List<string> in a class 实体框架代码类库中的第一个 - Entity Framework Code First in class library 实体框架代码的第一类关系 - Entity framework code first class relationships C#实体框架与该类中相同类的代码优先关系 - C# Entity Framework code-first relationship to same class within the class 实体框架代码首先为映射到相同类的不同表返回相同数据 - Entity Framework Code First returning same data for different tables mapped to same class 实体框架代码优先-对象全部继承基类,如何排除具有禁用属性的记录 - Entity Framework Code First - objects all inherit base class, how to exclude records with a disabled property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM