ObjectStateManager クラス

定義

エンティティ型インスタンスとリレーションシップ インスタンスのオブジェクトの状態と ID 管理を維持します。

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

次の例では、ObjectContextからObjectStateManagerを取得し、状態マネージャーを使用してコンテキスト内のオブジェクトにアクセスします。

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");
    }
}

次の例では、返されたObjectStateManagerTryGetObjectStateEntry(EntityKey, ObjectStateEntry) メソッドを使用して、エンティティ キーに基づいてオブジェクトを取得します。

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 はクエリ結果を追跡し、複数の重複するクエリ結果をマージするロジックを提供します。 また、ユーザーがオブジェクトを挿入、削除、または変更するときにメモリ内の変更追跡を実行し、更新プログラムの変更セットを提供します。 この変更セットは、変更を保持するために変更プロセッサによって使用されます。

このクラスは通常、アプリケーションで直接ではなく、 ObjectContext によって使用されます。

コンストラクター

名前 説明
ObjectStateManager(MetadataWorkspace)

ObjectStateManager クラスの新しいインスタンスを初期化します。

プロパティ

名前 説明
MetadataWorkspace

この状態マネージャーに関連付けられている MetadataWorkspace を取得します。

メソッド

名前 説明
ChangeObjectState(Object, EntityState)

特定のオブジェクトの ObjectStateEntry の状態を、指定した entityStateに変更します。

ChangeRelationshipState(Object, Object, String, EntityState)

2 つの関連オブジェクトとナビゲーション プロパティの名前に基づいて指定された 2 つのエンティティ オブジェクト間のリレーションシップの状態を変更します。

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

2 つの関連オブジェクトとリレーションシップのプロパティに基づいて指定された 2 つのエンティティ オブジェクト間のリレーションシップの状態を変更します。

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

2 つの関連オブジェクトと、ナビゲーション プロパティを定義する LINQ 式に基づいて指定された 2 つのエンティティ オブジェクト間のリレーションシップの状態を変更します。

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)

指定したEntityKeyとのオブジェクトまたはリレーションシップの対応するObjectStateEntryの取得を試みます。

TryGetObjectStateEntry(Object, ObjectStateEntry)

指定したObjectの対応するObjectStateEntryの取得を試みます。

TryGetRelationshipManager(Object, RelationshipManager)

指定したオブジェクトによって使用される RelationshipManager を返します。

イベント

名前 説明
ObjectStateManagerChanged

エンティティが状態マネージャーに追加されるか、状態マネージャーから削除されるときに発生します。

適用対象