无法跟踪实体类型 'xTestType' 的实例,因为已跟踪具有相同键的该类型的另一个实例?
The instance of entity type 'xTestType' cannot be tracked because another instance of this type with the same key is already being tracked?
我正在尝试从 table 中删除多行。但它在第一次迭代后给出了以下错误。我可以在所有 xTestType
对象上看到主键 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);
}
}
异常:
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 (i.e. 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.
存在此问题是因为 entity framework 在您获取所有这些数据时正在跟踪 xTestType
。有两种方法可以处理这种情况。
方法一:
DbContext.Entry(xTestTypeOld).State = EntityState.Deleted; // where xTestTypeOldis record from which you are taking xId
方法 2:
DbContext.Entry(xTestTypeOld).State = EntityState.Detached;
DbContext.Entry(xTestType).State = EntityState.Deleted;
我会说第一种方法是最好的。
当您从数据库中获取数据并对其进行迭代时,例如:
var dataObject = await dbContext.Table.where(x=>x.UserId == model.UserId).TolistAsync();
foreach(var item in dataObject)
{
}
不要创建另一个对象,将获取的对象直接传递给 Delete 或使用它来更新,因为 DbContext
正在跟踪它获取的对象,而不是您创建的对象。例如:
//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);
}
您必须将最初获取的实例传递给 Remove() 方法,请参阅:
var dataObject = await dbContext.Table.where(x=>x.UserId == model.UserId).TolistAsync();
foreach(var item in dataObject)
{
dbContext.Table.Remove(item);
}
我正在尝试从 table 中删除多行。但它在第一次迭代后给出了以下错误。我可以在所有 xTestType
对象上看到主键 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);
}
}
异常:
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 (i.e. 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.
存在此问题是因为 entity framework 在您获取所有这些数据时正在跟踪 xTestType
。有两种方法可以处理这种情况。
方法一:
DbContext.Entry(xTestTypeOld).State = EntityState.Deleted; // where xTestTypeOldis record from which you are taking xId
方法 2:
DbContext.Entry(xTestTypeOld).State = EntityState.Detached;
DbContext.Entry(xTestType).State = EntityState.Deleted;
我会说第一种方法是最好的。
当您从数据库中获取数据并对其进行迭代时,例如:
var dataObject = await dbContext.Table.where(x=>x.UserId == model.UserId).TolistAsync();
foreach(var item in dataObject)
{
}
不要创建另一个对象,将获取的对象直接传递给 Delete 或使用它来更新,因为 DbContext
正在跟踪它获取的对象,而不是您创建的对象。例如:
//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);
}
您必须将最初获取的实例传递给 Remove() 方法,请参阅:
var dataObject = await dbContext.Table.where(x=>x.UserId == model.UserId).TolistAsync();
foreach(var item in dataObject)
{
dbContext.Table.Remove(item);
}