ObjectStateManager クラス
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
エンティティ型インスタンスとリレーションシップ インスタンスのオブジェクトの状態と 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");
}
}
次の例では、返されたObjectStateManagerのTryGetObjectStateEntry(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 を取得します。 |
メソッド
イベント
| 名前 | 説明 |
|---|---|
| ObjectStateManagerChanged |
エンティティが状態マネージャーに追加されるか、状態マネージャーから削除されるときに発生します。 |