EF Core The instance of entity type cannot be tracked
程序员文章站
2022-04-25 21:25:19
...
记录一下EF core遇到:
Exception Message: The instance of entity type 'Entity' cannot be
tracked because another instance with the same key value for {'PropertyId'} is already being tracked.
EF单次的请求里,SaveChange不会释放entity的track
原代码:
context.PropertyEntity.UpdateRange(needUpdatedEntities);
修改为:
foreach(var updateProperty in needUpdatedProperties)
{
var updateEntity = await context.DealerCustomerProperty.FirstOrDefaultAsync(a => a.PropertyId == updateProperty.PropertyId);
if (updateEntity == null)
{
throw new ApplicationCoreException(ExceptionModule.Customer, ExceptionType.RecordNotExisted, ExceptionLevel.Error, ExceptionLayer.Repository);
}
_mapper.Map(updateProperty, updateEntity);
}