EF: Create/Remove relation from Many-to-Many relations when `AutoDetectChangesEnabled` and `ProxyCreationEnabled` are disabled on DbContext(EF:在 DbContext 上禁用“AutoDetectChangesEnabled和“ProxyCreationEnabled时从多对多关系创建/删除关系)
问题描述
知道
Foo.Id
和Bar.Id
如何在不从数据库加载实体的情况下创建它们的关系.
Knowning
Foo.Id
andBar.Id
how can I create their relation without loading the entities from the DB.
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
构造函数中也被禁用:
Also this configuration are disabled in DbContext
constructor:
Configuration.AutoDetectChangesEnabled = false;
Configuration.ProxyCreationEnabled = false;
Configuration.LazyLoadingEnabled = false;
以及如何消除这种关系?
And how it is possible to remove the relationship?
<小时>
例子:
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();
}
如何在不更改配置的情况下在这里定义关系.
How can I define relation here, without changing the configuration.
提前谢谢你.
推荐答案
好的,找到了解决方案,这里是辅助方法:
Ok, have found the solution and here is the helper method:
static void ChangeRelationship<T1, T2>(
IObjectContextAdapter ctx,
T1 a,
T2 b,
Expression<Func<T1, object>> getNavigationProperty,
EntityState state) where T1: class
{
ctx
.ObjectContext
.ObjectStateManager
.ChangeRelationshipState(
a,
b,
getNavigationProperty,
state
);
}
并在我的问题示例中使用它:
And using it in my example from the question:
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>() };
ctx.Entry(foo).State = EntityState.Unchanged;
ctx.Entry(bar).State = EntityState.Unchanged;
// create
ChangeRelationship(ctx, foo, bar, x => x.Bars, EntityState.Added);
ctx.SaveChanges();
// remove
ChangeRelationship(ctx, foo, bar, x => x.Bars, EntityState.Deleted);
ctx.SaveChanges();
}
这篇关于EF:在 DbContext 上禁用“AutoDetectChangesEnabled"和“ProxyCreationEnabled"时从多对多关系创建/删除关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:EF:在 DbContext 上禁用“AutoDetectChangesEnabled"和“ProxyCreationEnabled"时从多对多关系创建/删除关系
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 输入按键事件处理程序 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- WebMatrix WebSecurity PasswordSalt 2022-01-01