使用MVC3 EF4.1“IEntityChangeTracker的多个实例无法引用实体对象”的持续问题

时间:2021-01-28 05:32:40

I posted this question previously and it explains what I'm doing pretty thoroughly: ASP.NET MVC3 and Entity Framework v4.1 with error An entity object cannot be referenced by multiple instances of IEntityChangeTracker

我之前发布过这个问题并且它解释了我正在做的非常彻底:ASP.NET MVC3和Entity Framework v4.1带有错误IEntityChangeTracker的多个实例无法引用实体对象

The problem is that this issue has resurfaced several times with the mini-cart, lost cart, checkout page, etc after I fixed this particular problem in the question above. The further issues have been related to this, but not necessarily easy to identify and took a considerable amount of time to troubleshoot, find, and fix. Rather than post my most current specific issue I'd rather find out if I'm doing something wrong in general. Either by storing the cart, which is an entity, in a Session, or a better way to detach it (detach method shown below), or is there an easier way to debug these types of issues? Here is an update to my detach method:

问题是在我解决了上述问题中的这个特定问题之后,这个问题已经重新浮出了几次迷你车,丢失的车,结账页等。进一步的问题与此有关,但不一定容易识别并花费大量时间进行故障排除,查找和修复。而不是发布我当前最具体的问题,我宁愿发现我是否做错了。通过将一个实体的购物车存储在会话中,还是更好的分离方法(下面显示的分离方法),还是有更简单的方法来调试这些类型的问题?这是我的分离方法的更新:

    public void DetachCart(Cart cart)
    {
        var objectContext = ((IObjectContextAdapter)context).ObjectContext;
        if (cart.Customer != null)
        { objectContext.Detach(cart.Customer); }
        if (cart.ShipFromAddress != null)
        {
            var shipFromAddress = cart.ShipFromAddress;
            objectContext.Detach(cart.ShipFromAddress);
            cart.ShipFromAddress = shipFromAddress;
        }
        if (cart.ShipToAddress != null)
        {
            var shipToAddress = cart.ShipToAddress;
            objectContext.Detach(cart.ShipToAddress);
            cart.ShipToAddress = shipToAddress;
        }
        if (cart.Lines != null && cart.Lines.Count > 0)
        {
            List<CartLine> lines = new List<CartLine>();
            foreach (var item in cart.Lines.ToList())
            { 
                objectContext.Detach(item);
                lines.Add(item);
            }
            cart.Lines = lines;
        }
        objectContext.Detach(cart);
    }

Thank you for any insight you could provide me on this issue. It's been a long painful road with this one.

感谢您就此问题向我提供的任何见解。这是一条漫长而痛苦的道路。

UPDATE It seems that a lot of my trouble stems from the fact that CartModelBinder leaves the cart in the attached state rather than the detached state. By changing that it has eliminated my current issue and removed several places where I had to detach to avoid this issue. However, my question "is there an easier way to detach all, or a way to debug/track these issues" still stands.

更新我的很多麻烦似乎源于CartModelBinder将车停在附加状态而不是分离状态的事实。通过改变它已经消除了我当前的问题并删除了我必须分离的几个地方以避免这个问题。但是,我的问题是“是否有更简单的方法来分离所有问题,或者调试/跟踪这些问题的方法”仍然存在。

1 个解决方案

#1


1  

There is one solution - don't use entities in your views or model binders. Use view models and convert them to entities only when you are going save data to the database. It can make your application more complex but it will save you a great amount of time when troubleshooting issues with leaked contexts, attaching and detaching.

有一种解决方案 - 不要在视图或模型绑定器中使用实体。仅当您要将数据保存到数据库时,才使用视图模型并将它们转换为实体。它可以使您的应用程序更复杂,但它可以在解决泄漏的上下文,附加和分离问题时节省大量时间。

#1


1  

There is one solution - don't use entities in your views or model binders. Use view models and convert them to entities only when you are going save data to the database. It can make your application more complex but it will save you a great amount of time when troubleshooting issues with leaked contexts, attaching and detaching.

有一种解决方案 - 不要在视图或模型绑定器中使用实体。仅当您要将数据保存到数据库时,才使用视图模型并将它们转换为实体。它可以使您的应用程序更复杂,但它可以在解决泄漏的上下文,附加和分离问题时节省大量时间。