很多朋友在使用EF CORE 时都会出现
{"Message":"The instance of entity type 'OtherColumn' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values.","DevelopmentMessage":"
最重要的是for {'Id'} is already being tracked.
是因为d在修改数据的时候对数据进行了一次查询才导致的
如下:
var model=userservices.Find(id);
model.name="测试";
userservices.update(model);//这里会报错
此时底层代码如果是这样就报错
dbContext.Set<T>().FirstOrDefault<T>(whereLambda);
应该修改如下就可以了
dbContext.Set<T>().AsNoTracking().FirstOrDefault<T>(whereLambda);