EF Core 中先查询再去更新报错异常解决 The instance of entity type 'xxx' cannot be tracked ...
具体报错:The instance of entity type 'XXXX' 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.
解决方案1:查询的时候使用:AsNoTracking(),此时是不带变动跟踪的实体查询。
解决方案2:更新数据时,我们手动去trace一次:EntityState.Detached。
浙公网安备 33010602011771号