csharp EF Core 同一个实体被多次 tracked
csharp EF Core 同一个实体被多次 tracked
具体体现为:
he instance of entity type ‘XXXX’ cannot be tracked because another instance with the same key value for {‘Key’} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached.
原因:在使用 ef 查询后,再使用 ef 更新参与了查询的实体,就会报此错误。
重现
1 | |
解决:AsNoTracking
1 | |
csharp EF Core 同一个实体被多次 tracked
https://taylorandtony.github.io/2025/02/02/csharp-EF-Core同一个实体被多次tracked/