【发布时间】:2016-09-26 17:01:44
【问题描述】:
-
知道
Foo.Id和Bar.Id如何在不从数据库加载实体的情况下创建它们的关系。class Foo { public int Id { get; set; } public Lst<Bar> Bars { get; set; } } class Bar { public int Id { get; set; } public Lst<Foo> Foos { get; set; } }DbContext构造函数中也禁用了此配置:Configuration.AutoDetectChangesEnabled = false; Configuration.ProxyCreationEnabled = false; Configuration.LazyLoadingEnabled = false; 以及如何消除这种关系?
例子:
using (var ctx = new DbCtx())
{
ctx.Configuration.LazyLoadingEnabled = false;
ctx.Configuration.ProxyCreationEnabled = false;
ctx.Configuration.AutoDetectChangesEnabled = false;
ctx.Database.Log += Console.WriteLine;
var foo = new Foo {Id = 1, Bars = new List<Bar>() };
var bar = new Bar { Id = 3, Foos = new List<Foo>() };
// This approach wont work, as AutoDetectChanges are disabled
ctx.Foos.Attach(foo);
ctx.Bars.Attach(bar);
foo.Bars.Add(bar);
ctx.SaveChanges();
}
如何在不更改配置的情况下在这里定义关系。
提前谢谢你。
【问题讨论】:
-
您的问题不清楚?你能提供更多信息吗?
-
@Sampath,我已经用我想避免的示例更新了这个问题,因为它对数据库提出了额外的请求。
标签: c# entity-framework entity-framework-6