确定实体是否附加到 dbContext 的最合理方法是什么?
当我尝试将实体附加到上下文时,出现异常
已经有一个具有相同键的对象 存在于 ObjectStateManager 中。这 ObjectStateManager 无法跟踪 多个对象具有相同的键
这是预期的行为。
但我想知道 ObjectStateManager 如何知道这一点? 我想先自己做一下这个检查
when i try to attach entity to context i get an exception
An object with the same key already
exists in the ObjectStateManager. The
ObjectStateManager cannot track
multiple objects with the same key
This is expected behaviour.
But i would like to know how ObjectStateManager knows that?
I would like to do this check by myself before
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您使用 DbContext API(您提到了 ef-code-first),您可以简单地使用:
或更复杂的
如果使用 ObjectContext API,您可以使用:
If you are using DbContext API (you mentioned ef-code-first) you can simply use:
or more complex
In case of ObjectContext API you can use:
这是一个扩展方法,用于从上下文中获取对象,而不必担心它是否已经附加:
只需调用:
Here's an extension method for getting the object from the context without having to worry about whether it is already attached:
Just call:
检查
连接前
check
before attaching
请注意,如果您的上下文中禁用了更改跟踪,则询问
ObjectStateManager
或ChangeTracker
可能会返回该对象不在中ObjectContext
即使它实际上已经在那里了。因此,如果您尝试附加此类对象,则会引发异常。如果更改跟踪被禁用,则有效事件。
如果您不知道对象的类型,有多种方法,要么使用反射定义方法,要么使用像这样的其他技术
int GetIdOf(objectentity){...}
或者您定义一个类使用的接口,例如
并以这种方式使用它:
Note that if change tracking is disabled on your context, asking the
ObjectStateManager
or theChangeTracker
might return that the object is not in theObjectContext
even if it is in fact already in there. Therefore, if you try to attach such object it will raise an exception.works event if change tracking is disabled.
if you do not know the type of the object, there is various approach, either you define a method using reflection or other techniques like this one
int GetIdOf(object entity){...}
Or you define an interface used by your classes like
and use it this way :
您可以使用“Any”扩展方法查询 dbContext:
you can query the dbContext with the "Any" extension method:
如果您像我一样从 EF Core 延迟加载场景到达这里,其中导航属性通过 DbSet.Include() 子句填充到数据层中,同时实体附加到 DbContext,然后该实体分离并传递到业务层,请考虑将类似的内容添加到 DbContext.OnConfiguring(DbContextOptionsBuilder optionsBuilder) 方法中:
错误将被忽略,并且将返回最初 Include()d 的值。
If you have arrived here, as I did, from an EF Core Lazy Loading scenario in which Navigation properties were filled in a data layer via DbSet.Include() clause(s) while the Entity was attached to a DbContext and then that Entity was detached and passed up to a business layer, consider adding something like this to your DbContext.OnConfiguring(DbContextOptionsBuilder optionsBuilder) method:
The error will be ignored and the values that were originally Include()d will be returned.