实体框架 - 多对多的关系 [英] entity framework - many to many relationship

查看:90
本文介绍了实体框架 - 多对多的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我尝试使用许多与EF流利的API一对多的关系。我有2 POCO类

 公共类项目
{
公众诠释专案编号{搞定;组; }

公共虚拟的ICollection<作者>作者{搞定;组; }

公共项目()
{
=作者新的List<作者>();
}
}

公共类作者
{
公众诠释AUTHORID {搞定;组; }

公共虚拟的ICollection<项目>项目{搞定;组; }

公共作者()
{
类项目=新的List<项目>();
}
}

和我映射很多与这一部分一对多的关系代码:

  ////多对多
modelBuilder.Entity<项目>()
。的hasMany<作者>(A => a.Authors)
.WithMany(p => p.Projects)
.MAP(M =>
{
M。 ToTable(ProjectAuthors);
m.MapLeftKey(专案编号);
m.MapRightKey(AUTHORID);
});

在此数据库创建的表ProjectsAuthors。这是我与本案关系映射的第一次尝试。



如果我忽略此映射它创建的表AuthorProject类似的架构。这是正确的bevahior?


解决方案

通过试验和错误,我发现以下。鉴于两班......

 公共类ACLASS 
{
公众诠释标识{搞定;组; }
公众的ICollection< BClass> BClasses {搞定;组; }
}

公共类BClass
{
公众诠释标识{搞定;组; }
公众的ICollection<&ACLASS GT; AClasses {搞定;组; }
}



...并没有流利的映射和的DbContext像这样...

 公共类MyContext:的DbContext 
{
公共DbSet<&ACLASS GT; AClasses {搞定;组; }
公共DbSet< BClass> BClasses {搞定;组; }
}



...创建的名称连接表的 BClassAClasses 。如果我改变了设置的顺序...

 公共类MyContext:的DbContext 
{
公DbSet< BClass> BClasses {搞定;组; }
公共DbSet<&ACLASS GT; AClasses {搞定;组; }
}



...的名义创建的连接表的变化的 AClassBClasses 并在表的变化键列的顺序。因此,连接表和键列的顺序的名称似乎取决于其中实体类是装到模型中的顺序 - 它可以是 DbSet 声明或如果有更多关系参与其他顺序 - 例如一些其它实体指的是 ACLASS



<。 p>在最后,它不会在所有问题,因为这样的许多一对多的关系是对称。如果你想拥有自己的连接表的名称,你可以在你已经做了流利的API指定它



所以,你的问题:是的,命名连接表 AuthorProjects 是正确的行为。如果该名称已经 ProjectAuthors 这将是正确的行为,以及虽然。


Hi I try use Many to Many relationship with EF Fluent API. I have 2 POCO classes.

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. It is my first attempt with this case of relationship mapping.

If I omitted this mapping it created table AuthorProject with similar schema. It is correct 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...

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. 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. 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.

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.

So, to your question: Yes, naming the join table AuthorProjects is correct behaviour. If the name had been ProjectAuthors it would be correct behaviour as well though.

这篇关于实体框架 - 多对多的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆