ObjectContext跟踪实体框架中的更改 [英] ObjectContext tracking changes in Entity Framwwork
问题描述
我需要知道在我的objectContext中是否有任何改变(包括附加和分离的实体)。我应该遍历我所有的ObjectSets中的所有实体,并检查它们的EntityState还是有较短的方法?
I need to know if in my objectContext any changes have been made (include attached and detached entities). Should I loop over all my entities in all my ObjectSets and check their EntityState or is there a shorter way?
推荐答案
已分离
实体是分离的=上下文不了解它们,并且无法跟踪其更改。您不能循环访问您的ObjectSet - 它将整个数据库加载到您的应用程序!如果你想知道由上下文使用跟踪的实体的状态:
Detached
entities are detached = context doesn't know about them and it cannot track its changes. You cannot loop through your ObjectSets - it would load whole your database to your application! If you want to know state of your entities tracked by the context use:
var entries = context.ObjectStateManager.GetObjectStateEntries(~EntityState.Detached);
这将收集您的 ObjectStateEntry
的集合每个条目代表单一跟踪实体或独立关联。
This will get your collection of ObjectStateEntry
where each entry represents single tracked entity or independent association.
这篇关于ObjectContext跟踪实体框架中的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!