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
要找到的物品鑰匙。
傳回
an Object that 是一個實體類型的實例。
例外狀況
參數 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嘗試從 ObjectStateManager中擷取具有指定 EntityKey 的物件。 如果物件目前尚未載入到物件上下文中,會執行查詢,嘗試從資料來源回傳該物件。
GetObjectByKey當找不到該物件時,會升起 。ObjectNotFoundException 為了避免處理此例外,請改用 該 TryGetObjectByKey 方法。
此方法會回傳狀態下的 Deleted 物件。
暫存金鑰無法用來從資料來源回傳物件。