实体框架许多一对多创建关系表"向后" [英] Entity framework many-to-many relation table created "backwards"

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

问题描述

我在与EF6和许多一对多的关系的问题。我有以下设置:

I'm having an issue with EF6 and many-to-many relations. I have a following setup:

public class Foo
{
    public int Id { get; set; }
    public virtual ICollection<Bar> Bars { get; set; }
    public virtual ICollection<SubBar> SubBars { get; set; }

    public Foo()
    {
        Bars = new HashSet<Bar>();
        SubBars = new HashSet<SubBar>();
    }
}

public class Bar
{
    public int Id { get; set; }
    public virtual ICollection<Foo> Foos { get; set; }

    public Bar()
    {
        Foos = new HashSet<Foo>();
    }
}

public class SubBar
{
    public int Id { get; set; }
    public virtual ICollection<Foo> Foos { get; set; }

    public SubBar()
    {
        Foos = new HashSet<Foo>();
    }
}

酒吧之间的关系正常工作,并且有一个称为表 BarFoos 在数据库中。然而 SubBar 反转之间的关系。有一个在DB FooSubBars 的表中调用基本上有正确的关系,而是预先加载,如 SubBar.AsQueryable()。包括(SB = &GT; sb.Foos)返回 EntityCommandExecutionException 与消息无效的对象名称dbo.SubBarFoos

The relation between Foo and Bar works properly, and there is a table called BarFoos in the DB. However the relation between Foo and SubBar is reversed. There is a table in the DB called FooSubBars which basically holds the correct relations, but eager loading, like SubBar.AsQueryable().Include(sb => sb.Foos) returns an EntityCommandExecutionException with a message Invalid object name dbo.SubBarFoos.

问题是:我如何反向关系表的名称,以便预先加载?

- 编辑 -

DB创建在迁移如下:

Question is: How do I reverse the relation table name to allow eager loading?
--Edit--
DB creation in the migration is as follows:

CreateTable(
            "dbo.BarFoos",
            c => new
                {
                    Bar_Id = c.Int(nullable: false),
                    Foo_Id = c.Int(nullable: false),
                })
            .PrimaryKey(t => new { t.Bar_Id, t.Foo_Id })
            .ForeignKey("dbo.Bars", t => t.Bar_Id, cascadeDelete: true)
            .ForeignKey("dbo.Foos", t => t.Foo_Id, cascadeDelete: true)
            .Index(t => t.Bar_Id)
            .Index(t => t.Foo_Id);

CreateTable(
            "dbo.FooSubBars",
            c => new
                {
                    Foo_Id = c.Int(nullable: false),
                    SubBar_Id = c.Int(nullable: false),
                })
            .PrimaryKey(t => new { t.Foo_Id, t.SubBar_Id })
            .ForeignKey("dbo.Foos", t => t.Foo_Id, cascadeDelete: true)
            .ForeignKey("dbo.SubBars", t => t.SubBar_Id, cascadeDelete: true)
            .Index(t => t.Foo_Id)
            .Index(t => t.SubBar_Id);

将在迁移替换表的名称就足够了?

Would replacing the table name in the migration be sufficient?

推荐答案

可以保证如下表名作为FooBars生成的,如果您覆盖OnModelCreating方法对你的DbContext:

You can ensure the table name is generated as "FooBars" if you override the OnModelCreating method on your DbContext as follows:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<Foo>().HasMany(f => f.Bars).WithMany(b => b.Foos)
            .Map(m => 
                m.ToTable("FooBars")
                // Optionally specify the key column names...
                .MapLeftKey("FooId") 
                .MapRightKey("BarId")
            );

    modelBuilder.Entity<Foo>().HasMany(f => f.SubBars).WithMany(sb => sb.Foos).Map(m => m.ToTable("FooSubBars"));
}

将在迁移产生这样的:

Which will produce this in the migration:

        CreateTable(
            "dbo.FooBar",
            c => new
                {
                    FooId = c.Int(nullable: false),
                    BarId = c.Int(nullable: false),
                })
            .PrimaryKey(t => new { t.FooId, t.BarId })
            .ForeignKey("dbo.Foos", t => t.FooId, cascadeDelete: true)
            .ForeignKey("dbo.Bars", t => t.BarId, cascadeDelete: true)
            .Index(t => t.FooId)
            .Index(t => t.BarId);

        CreateTable(
            "dbo.FooSubBar",
            c => new
                {
                    Foo_Id = c.Int(nullable: false),
                    SubBar_Id = c.Int(nullable: false),
                })
            .PrimaryKey(t => new { t.Foo_Id, t.SubBar_Id })
            .ForeignKey("dbo.Foos", t => t.Foo_Id, cascadeDelete: true)
            .ForeignKey("dbo.SubBars", t => t.SubBar_Id, cascadeDelete: true)
            .Index(t => t.Foo_Id)
            .Index(t => t.SubBar_Id);

我也试过渴望在与查询的DbContext时SubBars没有得到任何错误加载FOOS:

I also tried eager loading the Foos when querying SubBars on the DbContext with don't get any errors:

var context = new FooBarContext();

var subBars = from sb in context.SubBars.Include(i => i.Foos)
              select sb;

这篇关于实体框架许多一对多创建关系表&QUOT;向后&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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