我只想更新客户端对象的行业属性。为此,我使用存储库从数据库检索客户端。这将导致跟踪我的行业(示例ID:149)。
var client = await _clientRepository.GetAsync(request.ClientId, token);
ind = _db.ChangeTracker.Entries<Industry>().Count();
然后,我通过调用SetIndustry
来更新ID为149(与以前相同)的新行业。
client.SetIndustry(new Industry(149));
ind = _db.ChangeTracker.Entries<Industry>().Count();
这时EF Core 2.1引发异常。
System.InvalidOperationException: 'The instance of entity type 'Industry' 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.
在此类中,我无法访问DBContext,因此无法使用变更跟踪器。我想知道如何解决这个问题?
Thx 塞巴
答案 0 :(得分:0)
当我创建文档变量并成功时,我刚刚将AsNoTracking()添加到文档对象中。尝试对您的客户端对象执行相同的操作。