gpt4 book ai didi

c# - Entity Framework 多对多关系表创建 "backwards"

转载 作者:太空狗 更新时间:2023-10-30 01:17:54 24 4
gpt4 key购买 nike

我遇到了 EF6 和多对多关系的问题。我有以下设置:

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>();
}
}

FooBar 之间的关系正常工作,并且在数据库中有一个名为BarFoos 的表。然而 FooSubBar 之间的关系是相反的。数据库中有一个名为 FooSubBars 的表,它基本上保持正确的关系,但需要预先加载,如 SubBar.AsQueryable().Include(sb => sb.Foos)返回带有消息 Invalid object name dbo.SubBarFoosEntityCommandExecutionException

问题是:如何反转关系表名称以允许预先加载?
--编辑--
迁移中的DB创建如下:

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);

替换迁移中的表名就足够了吗?

最佳答案

如果您重写 DbContext 上的 OnModelCreating 方法,则可以确保生成的表名称为“FooBars”,如下所示:

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"));
}

这将在迁移中产生这个:

        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,但没有出现任何错误:

var context = new FooBarContext();

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

关于c# - Entity Framework 多对多关系表创建 "backwards",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30101272/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com