c# – 实体框架多对多关系表创建“向后”
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 实体框架多对多关系表创建“向后”,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2897字,纯文字阅读大概需要5分钟。
内容图文
![c# – 实体框架多对多关系表创建“向后”](/upload/InfoBanner/zyjiaocheng/795/d3212f5bde644d91bc8af4c2be768994.jpg)
我遇到了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>();
}
}
Foo和Bar之间的关系正常工作,DB中有一个名为BarFoos的表.然而,Foo和SubBar之间的关系是相反的.数据库中有一个名为FooSubBars的表,它基本上保持正确的关系,但是像SubBar.AsQueryable()一样急切加载.Include(sb => sb.Foos)返回一个EntityCommandExecutionException,其中包含一个消息Invalid object name dbo.SubBarFoos.
问题是:如何反转关系表名称以允许急切加载?
– 编辑 –
迁移中的数据库创建如下:
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# – 实体框架多对多关系表创建“向后”全部内容,希望文章能够帮你解决c# – 实体框架多对多关系表创建“向后”所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。