ObjectContext.GetObjectByKey(EntityKey) メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
指定したエンティティ キーを持つオブジェクトを返します。
public:
System::Object ^ GetObjectByKey(System::Data::EntityKey ^ key);
public object GetObjectByKey(System.Data.EntityKey key);
member this.GetObjectByKey : System.Data.EntityKey -> obj
Public Function GetObjectByKey (key As EntityKey) As Object
パラメーター
- key
- EntityKey
見つかるオブジェクトのキー。
返品
エンティティ型のインスタンスである Object 。
例外
key パラメーターはnull。
オブジェクトが ObjectStateManager またはデータ ソースに見つかりません。
例
この例では、指定した型のエンティティの EntityKey を作成し、キーでエンティティをフェッチします。
using (AdventureWorksEntities context =
new AdventureWorksEntities())
{
try
{
// Define the entity key values.
IEnumerable<KeyValuePair<string, object>> entityKeyValues =
new KeyValuePair<string, object>[] {
new KeyValuePair<string, object>("SalesOrderID", 43680) };
// Create the key for a specific SalesOrderHeader object.
EntityKey key = new EntityKey("AdventureWorksEntities.SalesOrderHeaders", entityKeyValues);
// Get the object from the context or the persisted store by its key.
SalesOrderHeader order =
(SalesOrderHeader)context.GetObjectByKey(key);
Console.WriteLine("SalesOrderID: {0} Order Number: {1}",
order.SalesOrderID, order.SalesOrderNumber);
}
catch (ObjectNotFoundException ex)
{
Console.WriteLine(ex.ToString());
}
}
注釈
GetObjectByKey は、指定した EntityKey を持つオブジェクトを ObjectStateManagerから取得しようとします。 オブジェクトが現在オブジェクト コンテキストに読み込まれていない場合は、データ ソースからオブジェクトを返そうとしてクエリが実行されます。
GetObjectByKey は、オブジェクトが見つからない場合に ObjectNotFoundException を生成します。 この例外を処理しないようにするには、代わりに TryGetObjectByKey メソッドを使用します。
このメソッドは、 Deleted 状態のオブジェクトを返します。
一時キーを使用してデータ ソースからオブジェクトを返すことはできません。