EF:在DbContext上禁用“AutoDetectChangesEnabled”和“ProxyCreationEnabled”时,从多对多关系创建/删除关系 [英] EF: Create/Remove relation from Many-to-Many relations when `AutoDetectChangesEnabled` and `ProxyCreationEnabled` are disabled on DbContext

查看:1585
本文介绍了EF:在DbContext上禁用“AutoDetectChangesEnabled”和“ProxyCreationEnabled”时,从多对多关系创建/删除关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 已知 Foo.Id Bar.Id 我如何创建他们的关系而不从DB加载实体。

  1. Knowning Foo.Id and Bar.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?






    示例:


    Example:

    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”时,从多对多关系创建/删除关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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