Compartir a través de


ObjectContext.CreateEntityKey(String, Object) Método

Definición

Crea la clave de entidad para un objeto específico o devuelve la clave de entidad si ya existe.

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

Parámetros

entitySetName
String

Nombre completo de la entidad establecida en la que pertenece el objeto de entidad.

entity
Object

Objeto para el que se recupera la clave de entidad.

Devoluciones

EntityKey del objeto .

Excepciones

Cuando cualquiera de los parámetros es null.

Cuando entitySetName está vacío.

O bien

Cuando el tipo del objeto no existe en el conjunto de entity entidades.

O bien

Cuando no entitySetName está completo.

Cuando la clave de entidad no se puede construir correctamente en función de los parámetros proporcionados.

Ejemplos

En este ejemplo, CreateEntityKey se usa para recuperar la clave de entidad de un objeto existente.

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();
    }
}

Comentarios

Si no existe para EntityKeyentity, el CreateEntityKey método crea una nueva clave para él.

Este método se usa para determinar si un objeto que tiene el mismo EntityKey objeto ya está asociado a .ObjectContext Si un objeto que tiene el mismo EntityKey objeto ya está asociado, se genera una excepción. Use el CreateEntityKey método para intentar recuperar el EntityKey del objeto desasociado antes de llamar al Attach método .

Se aplica a