天天看點

ef 實體跟蹤的函數

xxx.Find()

xxx.First()

xxx.ToArray()

...

https://www.zhihu.com/question/388290065

Entity Framework Code First中能夠自動調用DbContext.ChangeTracker.DetectChanges的方法:

  ◊ DbSet.Add

  ◊ DbSet.Find

  ◊ DbSet.Remove

  ◊ DbSet.Attach

  ◊ DbSet.Local

  ◊ DbContext.SaveChanges

  ◊ DbContext.GetValidationErrors

  ◊ DbContext.Entry

  ◊ DbChangeTracker.Entries

  ◊ 任何在DbSet上進行LINQ的查詢

https://blog.csdn.net/u011127019/article/details/53941235

同一個上下文中,更新時,同一實體隻能跟蹤一次,如果再次擷取該對象并且跟蹤,并更新該對象時就會報錯,因為ef無法确定具體那個跟蹤的對象需要被儲存,錯誤如下

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會跟蹤對象,導緻更新失敗