简体   繁体   English

实体框架 - 多对多的关系

[英]entity framework - many to many relationship

Hi I try use Many to Many relationship with EF Fluent API. 您好,我尝试与EF Fluent API使用多对多关系。 I have 2 POCO classes. 我有2个POCO课程。

public class Project
{
    public int ProjectId { get; set; }

    public virtual ICollection<Author> Authors { get; set; }

    public Project()
    {
        Authors = new List<Author>();
    }
}

public class Author
{
    public int AuthorId { get; set; }

    public virtual ICollection<Project> Projects { get; set; }

    public Author()
    {
        Projects = new List<Project>();
    }
}

And I map many to many relationship with this part of code: 我用这部分代码映射了很多很多关系:

        ////MANY TO MANY 
        modelBuilder.Entity<Project>()
            .HasMany<Author>(a => a.Authors)
            .WithMany(p => p.Projects)
            .Map(m =>
                     {
                         m.ToTable("ProjectAuthors");
                         m.MapLeftKey("ProjectId");
                         m.MapRightKey("AuthorId");
                     });

This created table ProjectsAuthors in DB. 这在DB中创建了ProjectsAuthors表。 It is my first attempt with this case of relationship mapping. 这是我对这种关系映射案例的第一次尝试。

If I omitted this mapping it created table AuthorProject with similar schema. 如果我省略了这个映射,它创建了具有类似模式的表AuthorProject。 It is correct bevahior? 这是正确的bevahior?

By trial and error I found the following. 经过反复试验,我发现了以下内容。 Given two classes... 鉴于两个班级......

public class AClass
{
    public int Id { get; set; }
    public ICollection<BClass> BClasses { get; set; }
}

public class BClass
{
    public int Id { get; set; }
    public ICollection<AClass> AClasses { get; set; }
}

...and no Fluent mapping and a DbContext like this... ...没有像这样的Fluent映射和DbContext ......

public class MyContext : DbContext
{
    public DbSet<AClass> AClasses { get; set; }
    public DbSet<BClass> BClasses { get; set; }
}

...the name of the created join table is BClassAClasses . ...创建的连接表的名称是BClassAClasses If I change the order of the sets... 如果我改变集合的顺序......

public class MyContext : DbContext
{
    public DbSet<BClass> BClasses { get; set; }
    public DbSet<AClass> AClasses { get; set; }
}

...the name of the created join table changes to AClassBClasses and the order of the key columns in the table changes as well. ...创建的连接表的名称更改为AClassBClasses ,表中的键列顺序也会更改。 So, the name of the join table and the order of the key columns seems to depend on the order in which the entity classes are "loaded" into the model - which can be the order of the DbSet declarations or another order if more relationship are involved - for example some other entity refering to AClass . 因此,连接表的名称和键列的顺序似乎取决于实体类“加载”到模型中的顺序 - 如果有更多关系,则可以是DbSet声明的顺序或其他顺序涉及 - 例如一些其他实体AClass

In the end, it doesn't matter at all, because such a many-to-many relationship is "symmetric". 最后,它根本不重要,因为这种多对多关系是“对称的”。 If you want to have your own name of the join table, you can specify it in Fluent API as you already did. 如果您想拥有自己的连接表名称,可以像以前一样在Fluent API中指定它。

So, to your question: Yes, naming the join table AuthorProjects is correct behaviour. 那么,对于您的问题:是的,命名连接表AuthorProjects是正确的行为。 If the name had been ProjectAuthors it would be correct behaviour as well though. 如果名称是ProjectAuthors那么它也是正确的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM