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会跟踪对象,导致更新失败

posted @ 2020-11-21 19:10  事理  阅读(280)  评论(0编辑  收藏  举报