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();
}
}
備註
若 EntityKey 不存在 entity,則該 CreateEntityKey 方法為其建立新的鍵。
此方法用來判斷具有相同 EntityKey 的物件是否已附加於 ObjectContext。 若物件 EntityKey 已附加相同,則會提出例外。 在呼叫該方法之前Attach,請先CreateEntityKey嘗試取得分離物件的 。EntityKey