EntityKey Constructores

Definición

Inicializa una nueva instancia de la clase EntityKey.

Sobrecargas

Nombre Description
EntityKey()

Inicializa una nueva instancia de la clase EntityKey.

EntityKey(String, IEnumerable<KeyValuePair<String,Object>>)

Inicializa una nueva instancia de la EntityKey clase con un nombre de conjunto de entidades y una colección genérica KeyValuePair .

EntityKey(String, IEnumerable<EntityKeyMember>)

Inicializa una nueva instancia de la EntityKey clase con un nombre de conjunto de entidades y una IEnumerable<T> colección de EntityKeyMember objetos .

EntityKey(String, String, Object)

Inicializa una nueva instancia de la EntityKey clase con un nombre de conjunto de entidades y un par de claves de entidad específico.

EntityKey()

Inicializa una nueva instancia de la clase EntityKey.

public:
 EntityKey();
public EntityKey();
Public Sub New ()

Se aplica a

EntityKey(String, IEnumerable<KeyValuePair<String,Object>>)

Inicializa una nueva instancia de la EntityKey clase con un nombre de conjunto de entidades y una colección genérica KeyValuePair .

public:
 EntityKey(System::String ^ qualifiedEntitySetName, System::Collections::Generic::IEnumerable<System::Collections::Generic::KeyValuePair<System::String ^, System::Object ^>> ^ entityKeyValues);
public EntityKey(string qualifiedEntitySetName, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string,object>> entityKeyValues);
new System.Data.EntityKey : string * seq<System.Collections.Generic.KeyValuePair<string, obj>> -> System.Data.EntityKey
Public Sub New (qualifiedEntitySetName As String, entityKeyValues As IEnumerable(Of KeyValuePair(Of String, Object)))

Parámetros

qualifiedEntitySetName
String

que String es el nombre del conjunto de entidades calificado por el nombre del contenedor de entidades.

entityKeyValues
IEnumerable<KeyValuePair<String,Object>>

Colección genérica KeyValuePair .

Cada par clave-valor tiene un nombre de propiedad como clave y el valor de esa propiedad como valor. Debe haber un par para cada propiedad que forme parte de .EntityKey El orden de los pares clave-valor no es importante, pero se debe incluir cada propiedad de clave. Los nombres de propiedad son nombres simples que no están calificados con un nombre de tipo de entidad o el nombre del esquema.

Ejemplos

En este ejemplo se muestra cómo crear y usar un EntityKey.

using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    try
    {
        // Create the key that represents the order.
        EntityKey orderKey =
            new EntityKey("AdventureWorksEntities.SalesOrderHeaders",
                "SalesOrderID", orderId);

        // Create the stand-in SalesOrderHeader object
        // based on the specified SalesOrderID.
        SalesOrderHeader order = new SalesOrderHeader();
        order.EntityKey = orderKey;

        // Assign the ID to the SalesOrderID property to matche the key.
        order.SalesOrderID = (int)orderKey.EntityKeyValues[0].Value;

        // Attach the stand-in SalesOrderHeader object.
        context.SalesOrderHeaders.Attach(order);

        // Create a new SalesOrderDetail object.
        // You can use the static CreateObjectName method (the Entity Framework
        // adds this method to the generated entity types) instead of the new operator:
        // SalesOrderDetail.CreateSalesOrderDetail(1, 0, 2, 750, 1, (decimal)2171.2942, 0, 0,
        //                                         Guid.NewGuid(), DateTime.Today));
        SalesOrderDetail detail = new SalesOrderDetail
        {
            SalesOrderID = orderId,
            SalesOrderDetailID = 0,
            OrderQty = 2,
            ProductID = 750,
            SpecialOfferID = 1,
            UnitPrice = (decimal)2171.2942,
            UnitPriceDiscount = 0,
            LineTotal = 0,
            rowguid = Guid.NewGuid(),
            ModifiedDate = DateTime.Now
        };

        order.SalesOrderDetails.Add(detail);

        context.SaveChanges();
    }
    catch (InvalidOperationException)
    {
        Console.WriteLine("Ensure that the key value matches the value of the object's ID property.");
    }
    catch (UpdateException)
    {
        Console.WriteLine("An error has occurred. Ensure that an object with the '{0}' key value exists.",
        orderId);
    }
}

Se aplica a

EntityKey(String, IEnumerable<EntityKeyMember>)

Inicializa una nueva instancia de la EntityKey clase con un nombre de conjunto de entidades y una IEnumerable<T> colección de EntityKeyMember objetos .

public:
 EntityKey(System::String ^ qualifiedEntitySetName, System::Collections::Generic::IEnumerable<System::Data::EntityKeyMember ^> ^ entityKeyValues);
public EntityKey(string qualifiedEntitySetName, System.Collections.Generic.IEnumerable<System.Data.EntityKeyMember> entityKeyValues);
new System.Data.EntityKey : string * seq<System.Data.EntityKeyMember> -> System.Data.EntityKey
Public Sub New (qualifiedEntitySetName As String, entityKeyValues As IEnumerable(Of EntityKeyMember))

Parámetros

qualifiedEntitySetName
String

que String es el nombre del conjunto de entidades calificado por el nombre del contenedor de entidades.

entityKeyValues
IEnumerable<EntityKeyMember>

Colección IEnumerable<T> de EntityKeyMember objetos con los que se va a inicializar la clave.

Se aplica a

EntityKey(String, String, Object)

Inicializa una nueva instancia de la EntityKey clase con un nombre de conjunto de entidades y un par de claves de entidad específico.

public:
 EntityKey(System::String ^ qualifiedEntitySetName, System::String ^ keyName, System::Object ^ keyValue);
public EntityKey(string qualifiedEntitySetName, string keyName, object keyValue);
new System.Data.EntityKey : string * string * obj -> System.Data.EntityKey
Public Sub New (qualifiedEntitySetName As String, keyName As String, keyValue As Object)

Parámetros

qualifiedEntitySetName
String

que String es el nombre del conjunto de entidades calificado por el nombre del contenedor de entidades.

keyName
String

que String es el nombre de la clave.

keyValue
Object

que Object es el valor de clave.

Ejemplos

En este ejemplo se muestra cómo crear y usar un EntityKey.

using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    try
    {
        // Create the key that represents the order.
        EntityKey orderKey =
            new EntityKey("AdventureWorksEntities.SalesOrderHeaders",
                "SalesOrderID", orderId);

        // Create the stand-in SalesOrderHeader object
        // based on the specified SalesOrderID.
        SalesOrderHeader order = new SalesOrderHeader();
        order.EntityKey = orderKey;

        // Assign the ID to the SalesOrderID property to matche the key.
        order.SalesOrderID = (int)orderKey.EntityKeyValues[0].Value;

        // Attach the stand-in SalesOrderHeader object.
        context.SalesOrderHeaders.Attach(order);

        // Create a new SalesOrderDetail object.
        // You can use the static CreateObjectName method (the Entity Framework
        // adds this method to the generated entity types) instead of the new operator:
        // SalesOrderDetail.CreateSalesOrderDetail(1, 0, 2, 750, 1, (decimal)2171.2942, 0, 0,
        //                                         Guid.NewGuid(), DateTime.Today));
        SalesOrderDetail detail = new SalesOrderDetail
        {
            SalesOrderID = orderId,
            SalesOrderDetailID = 0,
            OrderQty = 2,
            ProductID = 750,
            SpecialOfferID = 1,
            UnitPrice = (decimal)2171.2942,
            UnitPriceDiscount = 0,
            LineTotal = 0,
            rowguid = Guid.NewGuid(),
            ModifiedDate = DateTime.Now
        };

        order.SalesOrderDetails.Add(detail);

        context.SaveChanges();
    }
    catch (InvalidOperationException)
    {
        Console.WriteLine("Ensure that the key value matches the value of the object's ID property.");
    }
    catch (UpdateException)
    {
        Console.WriteLine("An error has occurred. Ensure that an object with the '{0}' key value exists.",
        orderId);
    }
}

Se aplica a