ObjectContext.CreateEntityKey(String, Object) メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
特定のオブジェクトのエンティティ キーを作成するか、エンティティ キーが既に存在する場合は返します。
public:
System::Data::EntityKey ^ CreateEntityKey(System::String ^ entitySetName, System::Object ^ entity);
public System.Data.EntityKey CreateEntityKey(string entitySetName, object entity);
member this.CreateEntityKey : string * obj -> System.Data.EntityKey
Public Function CreateEntityKey (entitySetName As String, entity As Object) As EntityKey
パラメーター
- entitySetName
- String
エンティティ オブジェクトが属するエンティティ セットの完全修飾名。
- entity
- Object
エンティティ キーを取得する対象のオブジェクト。
返品
オブジェクトの EntityKey 。
例外
いずれかのパラメーターが null場合。
entitySetNameが空の場合。
-又は-
entity オブジェクトの型がエンティティ セットに存在しない場合。
-又は-
entitySetNameが完全修飾されていない場合。
指定されたパラメーターに基づいてエンティティ キーを正常に構築できない場合。
例
この例では、 CreateEntityKey を使用して、既存のオブジェクトのエンティティ キーを取得します。
private static void ApplyItemUpdates(SalesOrderDetail updatedItem)
{
// Define an ObjectStateEntry and EntityKey for the current object.
EntityKey key = default(EntityKey);
object originalItem = null;
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
// Create the detached object's entity key.
key = context.CreateEntityKey("SalesOrderDetails", updatedItem);
// Get the original item based on the entity key from the context
// or from the database.
if (context.TryGetObjectByKey(key, out originalItem))
{
// Call the ApplyCurrentValues method to apply changes
// from the updated item to the original version.
context.ApplyCurrentValues(key.EntitySetName, updatedItem);
}
context.SaveChanges();
}
}
注釈
entityのEntityKeyが存在しない場合、CreateEntityKey メソッドによって新しいキーが作成されます。
このメソッドは、同じ EntityKey を持つオブジェクトが既に ObjectContextにアタッチされているかどうかを判断するために使用されます。 同じ EntityKey を持つオブジェクトが既にアタッチされている場合は、例外が発生します。 Attach メソッドを呼び出す前に、CreateEntityKey メソッドを使用してデタッチされたオブジェクトのEntityKeyを取得します。