简体   繁体   English

无法跟踪实体类型“xTestType”的实例,因为已跟踪具有相同键的此类型的另一个实例?

[英]The instance of entity type 'xTestType' cannot be tracked because another instance of this type with the same key is already being tracked?

I'm trying to delete multiple rows from a table.我正在尝试从表中删除多行。 But it gives the following error after the first iteration.但它在第一次迭代后给出以下错误。 I can see primary key Id as 0 on all the xTestType object.我可以在所有xTestType对象上看到主键Id0 that might be the issue.这可能是问题所在。 Why is it always giving Id 0 .为什么它总是给Id 0

foreach (var temp in oldxDetails.TestTypes)
{
  if (deleteTestTypes.Contains(input.Id))
  {
    var xTestType = new xTestType
    {
      xId = xId,
      TestTypeMasterId = temp.Id
    };

    await _xTestRepository.DeleteAsync(xTestType);
  }
}

Exception:例外:

The instance of entity type 'xTestType' cannot be tracked because another instance of this type with the same key is already being tracked. When adding new entities, for most key types a unique temporary key value will be created if no key is set (ie if the key property is assigned the default value for its type). If you are explicitly setting key values for new entities, ensure they do not collide with existing entities or temporary values generated for other new entities. When attaching existing entities, ensure that only one entity instance with a given key value is attached to the context.

When you fetch data from database, and iterate over it, like:当您从数据库中获取数据并对其进行迭代时,例如:

var dataObject = await dbContext.Table.where(x=>x.UserId == model.UserId).TolistAsync();
foreach(var item in dataObject)
{ 

}

do not create another object, pass the fetched object directly to Delete or use it to update, because DbContext is tracking the objects it has fetched, not the ones you create.不要创建另一个对象,将获取的对象直接传递给 Delete 或使用它来更新,因为DbContext正在跟踪它已获取的对象,而不是您创建的对象。 for example:例如:

//Wrong code
var dataObject = await dbContext.Table.where(x=>x.UserId == model.UserId).TolistAsync();
foreach(var item in dataObject)
{ 
   var x=new DataObject()
   {
      x=item.Id
   };
   dbContext.Table.Remove(x);
}

you must pass the originally fetched instance to Remove() method, see:您必须将最初获取的实例传递给 Remove() 方法,请参阅:

var dataObject = await dbContext.Table.where(x=>x.UserId == model.UserId).TolistAsync();
foreach(var item in dataObject)
{ 
    dbContext.Table.Remove(item);
}

The issue exists because the entity framework is tracking xTestType when you fetched all of them.问题存在是因为实体框架在您获取所有这些时正在跟踪xTestType There are two approaches to handle the situation.有两种方法可以处理这种情况。

Approach 1:方法一:

DbContext.Entry(xTestTypeOld).State = EntityState.Deleted;  // where xTestTypeOldis record from which you are taking xId

Approach 2 :方法二:

DbContext.Entry(xTestTypeOld).State = EntityState.Detached;
DbContext.Entry(xTestType).State = EntityState.Deleted;

I would say the first approach is the best one.我会说第一种方法是最好的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 EF:无法跟踪实体类型X的实例,因为已经跟踪了具有相同密钥的此类型的另一个实例 - EF: The instance of entity type X cannot be tracked because another instance of this type with the same key is already being tracked 实体类型的实例……无法跟踪,因为已经在跟踪具有相同键的此类型的另一个实例 - The instance of entity type … cannot be tracked because another instance of this type with the same key is already being tracked 无法跟踪实体类型“ SalesOrder”的实例,因为已经跟踪了具有相同键的该类型的另一个实例 - The instance of entity type 'SalesOrder' cannot be tracked because another instance of this type with the same key is already being tracked 无法跟踪实体类型“ TestType”的实例,因为已经跟踪了具有相同键的该类型的另一个实例 - The instance of entity type 'TestType' cannot be tracked because another instance of this type with the same key is already being tracked 无法跟踪实体类型的实例,因为已在跟踪具有相同键的该类型的另一个实例 - The instance of entity type cannot be tracked because another instance of this type with the same key is already being tracked 无法跟踪实体类型“Item”的实例,因为已跟踪另一个具有与 {'Id'} 相同键值的实例 - The instance of entity type 'Item' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked 无法跟踪实体类型的实例,因为已在跟踪具有相同键值的另一个实例 - The instance of entity type cannot be tracked because another instance with the same key value is already being tracked 无法跟踪实体类型“书签”的实例,因为已经在跟踪具有相同键值 {'ID'} 的另一个实例 - The instance of entity type 'Bookmark' cannot be tracked because another instance with the same key value for {'ID'} is already being tracked 无法跟踪实体类型的实例,因为已跟踪具有相同键值的另一个实例 - The instance of entity type cannot be tracked because another instance with the same key value for is already being tracked 实体类型的实例 <T> 无法跟踪,因为已经跟踪了另一个具有相同的{&#39;Id&#39;}键值的实例 - The instance of entity type <T> cannot be tracked because another instance with the same key value for {'Id'} is already being tracked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM