# EF更新报错

EF更新时报错:出现这种原因是因为两个不同的对象使用了同一个id值
The instance of entity type 'XXX' cannot be 
tracked because another instance with the same key value for {'XX'} is already being tracked.

//解决办法1:
更新前请调用:表Repository.Instance.Detached(对象)或repository.Instance.Detached(对象)

//解决办法2,查询时设置不跟踪
var data = repository.DbContext.Set<>().AsNoTracking().Where(x => 条件).FirstOrDefault();
1
2
3
4
5
6
7
8
9