c# - 如何将 DbContext (EntityFramework SqlConnection) 传递给另一个方法?

标签 c# entity-framework sqlite entity-framework-6 system.data.sqlite

将 DbContext 传递给另一个方法的问题,例如:

public bool MarkCustomerForDelete(Customer customerObj)
{
        using(var dbContext = new MyContext())
        {
            using(var dbTransaction = dbContext.Database.BeginTransaction())
            {

                //Clear all orders for the Given Customers          
                var orderList = dbContext.Orders.Where(x=>x.id == customerObj.OrderId).ToList();

                CommonLogicMethod(dbContext, orderList);

                //Logic 
                customerObj.Status = "Deleted";
// The Modification will fail over due to the Customer Object for that object is already attached to the DbContext with Previous Values
                dbContext.Entry(customerObj).State = EntityState.Modified;

                dbContext.SaveChanges();
                dbTransaction.Commit()
                return true;
            }
        }
}
public void DeleteOrderRelatedData(MyContext dbContext, List<Orders> orderList)
{
    foreach(var entity2 in entity2List)
    {
        var OrderAddresses = dbContext.OrderAddresses.Where(x=>x.Id == entity2.Id).ToList();
        //Now if here the dbContext has 100 Entities (Tables)
        //It internally Enumerates all the entities in the Local cache i.e. dbContext.Coupons.Local has all the Records from the DB in the Local present.


    }
}

问题:为什么当 DbContext 被传递给另一个方法时,会在内部调用所有数据,即 dbContext.Customers.Local 中的所有数据都在一级缓存中的数据库中?

问题:如何将 DbContext 从一种方法传递到另一种方法(不创建上述问题)?

这是创建与修改数据相关的问题,即 DeleteCustomer 将故障转移。
现在,如果将 DeleteOrderRelatedData 中的代码合并到 DeleteCustomer 函数中,它就可以正常工作。

我为 dbContext 添加了一个日志,并且 dbContext 在内部将其传递给函数时正在调用与不同查询相关的所有 Select 查询。

更多详情,请观看此视频:Link

正在使用的工具:
  • Entity Framework 6.0
  • System.Data.Sqlite
  • PostSharp 用于 MethodBoundary 方面。
  • 最佳答案

    听起来你的问题与级联删除有关,但措辞很难理解......

    您问题中的陈述...

    DbContext is passed to another method internally calls for all the data



    ... DbContexts 不只是自动“去获取所有数据”,你必须触发一些导致它的东西。

    在我看来,当您删除客户对象 EF 时,您正在手动实现级联删除的代码,而您可能应该做的只是将其添加到模型中,然后删除客户对象,从而不需要所有这些额外的逻辑.

    换句话说,您已经说过/正在尝试说“当删除客户时,还要查找并删除与客户相关的订单”。

    在上面的代码示例中,您可以...
    //Clear all orders for the Given Customers          
    var orderList = dbContext.Orders.Where(x=>x.id == customerObj.OrderId).ToList();
    

    这纯粹是通过执行“select * from orders where customerid = customer.Id”来获取订单

    然后在您在下面定义的方法中...
    public void DeleteOrderRelatedData(MyContext dbContext, List<Orders> orderList)
    

    ...看起来您想进一步删除订单的所有地址。尽管您似乎没有在上面的示例中调用该方法。

    相反,您可以做这样的事情让 EF 担心数据库中的所有子孙删除...

    Entity Framework (EF) Code First Cascade Delete for One-to-Zero-or-One relationship

    Cascading deletes with Entity Framework - Related entities deleted by EF

    Microsoft 文档在这里...

    https://msdn.microsoft.com/en-gb/data/jj591620.aspx

    编辑:

    我的回答是基于我知道 EF 会开箱即用的做法,似乎实际问题是由问题中未提及的组件引起的,问题不在于执行我所解释的操作层次结构关于解决另一个第三方组件如何对 EF 行为产生不利影响的问题的事实。

    针对这个问题:

    如何将 DbContext 从一种方法传递到另一种方法(不创建上述给定问题)?

    ...只是这样做,因为在 2 个方法之间传递上下文本身不会导致您遇到的问题。

    ...

    看来要正确回答这个问题是不可能的:(

    关于c# - 如何将 DbContext (EntityFramework SqlConnection) 传递给另一个方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36033481/

    相关文章:

    c++ - 如何编译动态库?

    安卓 : SQLiteException: not an error not on all devices

    c# - Base64 解码带有 ISO-8859-1 字符集的字符串并将其保存到 NSData

    c# - (someDelegateName)?.Invoke(); 中 "?"的用途是什么?

    c# - 集合初始化语法之间的差异

    c# - 从 Entity Framework 调用复杂的存储过程?

    entity-framework - 您可以将 Microsoft Entity Framework 与 Postgres 一起使用吗?

    c# - 查找集合的 NHibernate 映射(如果这有意义的话)

    c# - 如何在桌面应用程序中使用 DbContext 和 DI?

    SQL 高效检查关联是否存在?