ObjectContext.CreateEntityKey(String, Object) 메서드
정의
특정 개체에 대한 엔터티 키를 만들거나 엔터티 키가 이미 있는 경우 해당 엔터티 키를 반환합니다.Creates the entity key for a specific object, or returns the entity key if it already exists.
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
엔터티 개체가 속한 엔터티 집합의 정규화된 이름입니다.The fully qualified name of the entity set to which the entity object belongs.
- entity
- Object
엔터티 키를 검색 중인 개체입니다.The object for which the entity key is being retrieved.
반환
개체의 EntityKey입니다.The EntityKey of the object.
예외
매개 변수 중 하나가 null
인 경우When either parameter is null
.
entitySetName
이 비어 있는 경우When entitySetName
is empty.
또는-or-
entity
개체의 형식이 엔터티 집합에 없는 경우When the type of the entity
object does not exist in the entity set.
또는-or-
entitySetName
이 정규화되어 있지 않은 경우When the entitySetName
is not fully qualified.
제공된 매개 변수를 기반으로 엔터티 키를 성공적으로 생성할 수 없는 경우When the entity key cannot be constructed successfully based on the supplied parameters.
예제
이 항목의 예제는 Microsoft SQL Server 제품 샘플: 데이터베이스를 기반으로 합니다.The example in this topic is based on the Microsoft SQL Server Product Samples: Database. 이 예제에서 CreateEntityKey 는 기존 개체의 엔터티 키를 검색 하는 데 사용 됩니다.In this example, CreateEntityKey is used to retrieve the entity key of an existing object.
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();
}
}
Private Shared Sub ApplyItemUpdates(ByVal updatedItem As SalesOrderDetail)
' Define an ObjectStateEntry and EntityKey for the current object.
Dim key As EntityKey
Dim originalItem As Object
Using context As 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, originalItem) Then
' Call the ApplyCurrentValues method to apply changes
' from the updated item to the original version.
context.ApplyCurrentValues(key.EntitySetName, updatedItem)
End If
context.SaveChanges()
End Using
End Sub
설명
EntityKey에 대 한가 없는 경우 entity
이 메서드는 해당에 CreateEntityKey 대 한 새 키를 만듭니다.If an EntityKey does not exist for the entity
, the CreateEntityKey method creates a new key for it.
이 메서드는가 같은 개체가에 이미 연결 되어 있는지 여부를 확인 하는 데 사용 됩니다 EntityKey ObjectContext .This method is used to determine whether an object that has the same EntityKey is already attached to the ObjectContext. 같은가 있는 개체가 EntityKey 이미 연결 되어 있으면 예외가 발생 합니다.If an object that has the same EntityKey is already attached, an exception is raised. 메서드를 CreateEntityKey 호출 하기 전에 메서드를 사용 하 여 분리 된 개체의를 검색 하려고 시도 합니다 EntityKey Attach .Use the CreateEntityKey method to try to retrieve the EntityKey of the detached object before calling the Attach method.