ef 實(shí)體跟蹤的函數(shù)
xxx.Find()
xxx.First()
xxx.ToArray()
...
https://www.zhihu.com/question/388290065
Entity Framework Code First中能夠自動調(diào)用DbContext.ChangeTracker.DetectChanges的方法:
? DbSet.Add
? DbSet.Find
? DbSet.Remove
? DbSet.Attach
? DbSet.Local
? DbContext.SaveChanges
? DbContext.GetValidationErrors
? DbContext.Entry
? DbChangeTracker.Entries
? 任何在DbSet上進(jìn)行LINQ的查詢
https://blog.csdn.net/u011127019/article/details/53941235
同一個(gè)上下文中,更新時(shí),同一實(shí)體只能跟蹤一次,如果再次獲取該對象并且跟蹤,并更新該對象時(shí)就會報(bào)錯(cuò),因?yàn)閑f無法確定具體那個(gè)跟蹤的對象需要被保存,錯(cuò)誤如下
The instance of entity type 'UserEntity' 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
https://q.cnblogs.com/q/103473/ 此問題中就是Find會跟蹤對象,導(dǎo)致更新失敗

浙公網(wǎng)安備 33010602011771號