EntityState 列舉


實體 (Entity) 物件的狀態。The state of an entity object.

此列舉有 FlagsAttribute 屬性,因此其成員值可進行位元組合。

public enum class EntityState
public enum EntityState
public enum EntityState
type 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. 已呼叫 Modified 方法時,在沒有變更追蹤 Proxy 的 POCO 實體中,已修改之屬性的狀態會變更為 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.