ObjectContext.CreateEntityKey(String, Object) Método

Definición

Crea la clave de entidad para un objeto determinado 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

El nombre completo del conjunto de entidades al que pertenece el objeto entidad.

entity
Object

Objeto cuya clave de entidad se recupera.

Devoluciones

El objeto EntityKey del objeto.

Excepciones

Cuando cualquiera de los parámetros es null.

Cuando entitySetName está vacío.

o bien

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

o bien

Cuando entitySetName no es un nombre completo.

Cuando la clave de entidad no se puede construir correctamente basándose en los parámetros proporcionados.

Ejemplos

En este ejemplo, se usa CreateEntityKey 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 entitypara EntityKey , el CreateEntityKey método crea una nueva clave para él.

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

Se aplica a