我收到了错误
IEntityChangeTracker的多个实例不能引用实体对象
尝试创建新实体并将其保存到数据库时.
我理解错误以及它通常如何发生,但在这种情况下,我所做的只是创建一个新实体并int在保存之前添加几个s,而不是从其他上下文添加任何其他实体.
我已经包含了导致错误的函数.正如你所看到的那样,它被传递了EndProduct一个实体,它被一个不同的上下文跟踪到了一个实体_billableRepository,但是由于我不想尝试将该实体分配给新创建的计费,我看不出如何这可能是个问题.
我能看到错误发生的唯一方法是因为int分配给new的Billable一些值是从EndProduct不同上下文跟踪的现有值中获取的,但肯定IEntityChangeTracker不会跟踪实体的各个原语?
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, …Run Code Online (Sandbox Code Playgroud)