EntityState 枚举


实体对象的状态。The state of an entity object.

此枚举有一个 FlagsAttribute 属性,允许按位组合成员值。

public enum class EntityState
public enum EntityState
type EntityState = 
Public Enum EntityState


Added 4

该对象是已添加到对象上下文的新对象,但尚未调用 SaveChanges() 方法。The object is new, has been added to the object context, and the SaveChanges() method has not been called. 在保存更改后,对象状态更改为 UnchangedAfter the changes are saved, the object state changes to Unchanged. 处于 Added 状态的对象在 ObjectStateEntry 中没有原始值。Objects in the Added state do not have original values in the ObjectStateEntry.

Deleted 8

已从对象上下文中删除该对象。The object has been deleted from the object context. 在保存更改后,对象状态更改为 DetachedAfter the changes are saved, the object state changes to Detached.

Detached 1

该对象存在但未被跟踪。The object exists but is not being tracked. 实体在创建之后且添加到对象上下文之前处于此状态。An entity is in this state immediately after it has been created and before it is added to the object context. 通过调用 Detach(Object) 方法从上下文中移除实体后,或者使用 NoTrackingMergeOption 加载实体后,该实体也会处于此状态。An entity is also in this state after it has been removed from the context by calling the Detach(Object) method or if it is loaded by using a NoTrackingMergeOption. 没有与处于 ObjectStateEntry 状态的对象关联的 Detached 实体。There is no ObjectStateEntry instance associated with objects in the Detached state.

Modified 16

对象上的一个标量属性已修改,但尚未调用 SaveChanges() 方法。One of the scalar properties on the object was modified and the SaveChanges() method has not been called. 在没有更改跟踪代理的 POCO 实体中,如果调用 Modified 方法,则已修改属性的状态将更改为 DetectChanges()In POCO entities without change-tracking proxies, the state of the modified properties changes to Modified when the DetectChanges() method is called. 在保存更改后,对象状态更改为 UnchangedAfter the changes are saved, the object state changes to Unchanged.

Unchanged 2

自附加到上下文中后,或自上次调用 SaveChanges() 方法后,该对象尚未修改。The object has not been modified since it was attached to the context or since the last time that the SaveChanges() method was called.


对象上下文必须了解对象的状态以便将更改保存回数据源。The object context must know the state of an object to save changes back to the data source. ObjectStateEntry 对象存储 EntityState 信息。ObjectStateEntry objects store EntityState information. SaveChangesObjectContext 方法将处理附加到上下文的实体,并根据每个对象的 EntityState 更新数据源。The SaveChanges methods of the ObjectContext process entities that are attached to the context and update the data source depending on the EntityState of each object. 有关详细信息, 请参阅创建、添加、修改和删除对象For more information, see Creating, Adding, Modifying, and Deleting Objects.

对象上下文中的对象状态由 ObjectStateManager 管理。The state of objects inside an object context is managed by the ObjectStateManager. 若要确定对象的状态,请调用以下 ObjectStateManager 方法之一:TryGetObjectStateEntryGetObjectStateEntryGetObjectStateEntriesTo find out the state of an object, call one of the following ObjectStateManager methods: TryGetObjectStateEntry, GetObjectStateEntry, or GetObjectStateEntries. StateObjectStateEntry 属性定义对象的状态。The State property of the ObjectStateEntry defines the state of the object.