ObjectContext.CreateEntityKey(String, Object) 메서드

정의

특정 개체에 대한 엔터티 키를 만들거나 엔터티 키가 이미 있는 경우 해당 엔터티 키를 반환합니다.

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

개체의 EntityKey입니다.

예외

매개 변수 중 하나가 null인 경우

entitySetName이 비어 있는 경우

또는

entity 개체의 형식이 엔터티 집합에 없는 경우

또는

entitySetName이 정규화되어 있지 않은 경우

제공된 매개 변수를 기반으로 엔터티 키를 성공적으로 생성할 수 없는 경우

예제

이 항목의 예제는 Microsoft SQL Server 제품 샘플: 데이터베이스를 기반으로 합니다. 이 예제에서는 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();
    }
}
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 새 키를 만듭니다.

이 메서드는 동일한 EntityKey 개체가 이미 연결되어 ObjectContext있는지 여부를 확인하는 데 사용됩니다. 동일한 EntityKey 개체가 이미 연결되어 있으면 예외가 발생합니다. 메서드를 CreateEntityKey 호출하기 전에 분리된 개체를 EntityKey 검색하려면 이 메서드를 Attach 사용합니다.

적용 대상