实体框架“实体对象不能被IEntityChangeTracker的多个实例引用” [英] Entity Framework "An entity object cannot be referenced by multiple instances of IEntityChangeTracker"

查看:101
本文介绍了实体框架“实体对象不能被IEntityChangeTracker的多个实例引用”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到错误


一个实体对象不能被
IEntityChangeTracker的多个实例引用

An entity object cannot be referenced by multiple instances of IEntityChangeTracker

当尝试创建一个新的实体并将其保存到数据库。

when trying to create a new entity and save it to the DB.

我了解错误以及通常发生的情况,但在这种情况下,我正在做的是创建一个新实体并添加几个 int s保存之前,不要从其他上下文添加任何其他实体。

I understand the error and how it normally occurs, but in this instance all I am doing is creating a new entity and adding a few ints to it before saving, not adding any other entities from other contexts.

我已经包括导致错误的函数。正如你所看到的那样,它正在传递一个 EndProduct ,它是一个被不同上下文跟踪到 _billableRepository ,但是由于我不是试图将该实体分配给新创建的可结算单,所以我看不出它可能是一个问题。

I have included the function that is causing the error. As you can see it is being passed an EndProduct which is an entity which is being tracked by a different context to the one in the _billableRepository, but since I am not trying to in anyway assign that entity to the newly created billable I don't see how it can be a problem.

我可以看到错误发生的唯一方法是因为两个分配给 int 的值新的可结算取自现有的 EndProduct ,它被不同的上下文跟踪,但肯定是 IEntityChangeTracker 不跟踪实体的单个基元?

The only way I can see the error happening is because a couple of the int values that are being assigned to the new Billable are taken from the existing EndProduct that is being tracked by a different context, but surely the IEntityChangeTracker doesn't track the individual primitives of an entity?

public void AddBillable(EndProduct endProduct, int? purchaseId, string centreCode, int userId)
{
    if (endProduct.Product != null)
    {
        var existingBillableForUserForProductId = _billableRepository.GetQuery(b => b.UserId == userId && b.ProductId == endProduct.ProductId);
        if (endProduct.BillablePartId != null && !existingBillableForUserForProductId.Any())
        {
            var billable = new Billable {
                ProductId = endProduct.ProductId.Value, //int
                UserId = userId, //int
                PartId = endProduct.BillablePartId.Value, //int
                DateAdded = DateTime.UtcNow, //datetime
                PurchaseId = purchaseId, //int 
                CentreCode = centreCode //string
            };

            _billableRepository.Add(billable); //error here
            _billableRepository.UnitOfWork.SaveChanges();
        }
    }
}


推荐答案

最可能的原因是与您使用的任何依赖注入工具。

The most likely cause of this is to do with any dependency injection tool you're using.

应该只有一个 DbContext 在单位工作中玩耍。如果您每次新建一个新的,请确保旧的已被处理。

There should only be one DbContext in play per unit of work. If you are newing up a new one each time, make sure the old one is disposed of.

否则,您将有多个相同上下文的实例一起运行

Otherwise, you will have multiple instances of the same context running alongside each other.

这是变更追踪程式所迷惑的地方,无法追踪您的实体变更。

This is where the change tracker gets confused and is unable to track changes to your entities.

这篇关于实体框架“实体对象不能被IEntityChangeTracker的多个实例引用”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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