ObjectStateManager 類別

定義

維護實體 (Entity) 類型執行個體 (Instance) 和關聯性 (Relationship) 執行個體的物件狀態與識別 (Identity) 管理。

public ref class ObjectStateManager
public class ObjectStateManager
type ObjectStateManager = class
Public Class ObjectStateManager
繼承
ObjectStateManager

範例

下列範例會從 ObjectStateManager 中取得 ObjectContext 並且使用此狀態管理員來存取內容中的物件。

int orderId = 43680;

using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    ObjectStateManager objectStateManager = context.ObjectStateManager;
    ObjectStateEntry stateEntry = null;

    var order = (from o in context.SalesOrderHeaders
                 where o.SalesOrderID == orderId
                 select o).First();

    // Attempts to retrieve ObjectStateEntry for the given EntityKey.
    bool isPresent = objectStateManager.TryGetObjectStateEntry(((IEntityWithKey)order).EntityKey, out stateEntry);
    if (isPresent)
    {
        Console.WriteLine("The entity was found");
    }
}

下列範例會針對傳回的 TryGetObjectStateEntry(EntityKey, ObjectStateEntry) 使用 ObjectStateManager 方法,以便根據實體索引鍵取得物件。

private static void ApplyItemUpdates(SalesOrderDetail originalItem,
    SalesOrderDetail updatedItem)
{
    using (AdventureWorksEntities context =
        new AdventureWorksEntities())
    {
        context.SalesOrderDetails.Attach(updatedItem);
        // Check if the ID is 0, if it is the item is new.
        // In this case we need to chage the state to Added.
        if (updatedItem.SalesOrderDetailID == 0)
        {
            // Because the ID is generated by the database we do not need to
            // set updatedItem.SalesOrderDetailID.
            context.ObjectStateManager.ChangeObjectState(updatedItem, System.Data.EntityState.Added);
        }
        else
        {
            // If the SalesOrderDetailID is not 0, then the item is not new
            // and needs to be updated. Because we already added the
            // updated object to the context we need to apply the original values.
            // If we attached originalItem to the context
            // we would need to apply the current values:
            // context.ApplyCurrentValues("SalesOrderDetails", updatedItem);
            // Applying current or original values, changes the state
            // of the attached object to Modified.
            context.ApplyOriginalValues("SalesOrderDetails", originalItem);
        }
        context.SaveChanges();
    }
}

備註

ObjectStateManager 會追蹤查詢結果,並提供邏輯來合併多個重疊的查詢結果。 它也會在使用者插入、刪除或修改物件時執行記憶體中的變更追蹤,並且提供更新的變更集。 變更處理器會使用這個變更集來保存修改項目。

這個類別 (Class) 通常是由 ObjectContext 所使用,而且不會直接使用在應用程式中。

建構函式

ObjectStateManager(MetadataWorkspace)

初始化 ObjectStateManager 類別的新執行個體。

屬性

MetadataWorkspace

取得與這個狀態管理員相關聯的 MetadataWorkspace

方法

ChangeObjectState(Object, EntityState)

將指定物件的 ObjectStateEntry 狀態變更成指定的 entityState

ChangeRelationshipState(Object, Object, String, EntityState)

變更兩個實體物件之間關聯性的狀態,其中的關聯性狀態是依據兩個相關物件和導覽屬性名稱而指定的。

ChangeRelationshipState(Object, Object, String, String, EntityState)

變更兩個實體物件之間關聯性的狀態,其中的關聯性狀態是依據兩個相關物件和關聯性屬性而指定的。

ChangeRelationshipState<TEntity>(TEntity, Object, Expression<Func<TEntity,Object>>, EntityState)

變更兩個實體物件之間關聯性的狀態,其中的關聯性狀態是依據兩個相關物件和定義導覽屬性的 LINQ 運算式而指定的。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetObjectStateEntries(EntityState)

傳回具有給定狀態之物件或關聯性的 ObjectStateEntry 物件集合。

GetObjectStateEntry(EntityKey)

傳回具有指定之索引鍵的物件或關聯性項目的 ObjectStateEntry

GetObjectStateEntry(Object)

傳回指定之物件的 ObjectStateEntry

GetRelationshipManager(Object)

傳回指定物件所使用的 RelationshipManager

GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)
TryGetObjectStateEntry(EntityKey, ObjectStateEntry)

嘗試擷取具有指定之 ObjectStateEntry 的物件或關聯性的對應 EntityKey

TryGetObjectStateEntry(Object, ObjectStateEntry)

嘗試擷取指定之 ObjectStateEntry 的對應 Object

TryGetRelationshipManager(Object, RelationshipManager)

傳回指定物件所使用的 RelationshipManager

事件

ObjectStateManagerChanged

在狀態管理員中加入或移除實體時發生。

適用於