EntityKey Costruttori

Definizione

Inizializza una nuova istanza della classe EntityKey.

Overload

Nome Descrizione
EntityKey()

Inizializza una nuova istanza della classe EntityKey.

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

Inizializza una nuova istanza della EntityKey classe con un nome del set di entità e una raccolta generica KeyValuePair .

EntityKey(String, IEnumerable<EntityKeyMember>)

Inizializza una nuova istanza della EntityKey classe con un nome del set di entità e una IEnumerable<T> raccolta di EntityKeyMember oggetti.

EntityKey(String, String, Object)

Inizializza una nuova istanza della EntityKey classe con un nome del set di entità e una coppia di chiavi di entità specifica.

EntityKey()

Inizializza una nuova istanza della classe EntityKey.

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

Si applica a

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

Inizializza una nuova istanza della EntityKey classe con un nome del set di entità e una raccolta generica 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)))

Parametri

qualifiedEntitySetName
String

Oggetto String che rappresenta il nome del set di entità qualificato dal nome del contenitore di entità.

entityKeyValues
IEnumerable<KeyValuePair<String,Object>>

Raccolta generica KeyValuePair .

Ogni coppia chiave/valore ha un nome di proprietà come chiave e il valore di tale proprietà come valore. Deve essere presente una coppia per ogni proprietà che fa parte di EntityKey. L'ordine delle coppie chiave/valore non è importante, ma ogni proprietà chiave deve essere inclusa. I nomi delle proprietà sono nomi semplici che non sono qualificati con un nome di tipo di entità o il nome dello schema.

Esempio

In questo esempio viene illustrato come creare e usare un oggetto 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);
    }
}

Si applica a

EntityKey(String, IEnumerable<EntityKeyMember>)

Inizializza una nuova istanza della EntityKey classe con un nome del set di entità e una IEnumerable<T> raccolta di EntityKeyMember oggetti.

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))

Parametri

qualifiedEntitySetName
String

Oggetto String che rappresenta il nome del set di entità qualificato dal nome del contenitore di entità.

entityKeyValues
IEnumerable<EntityKeyMember>

Raccolta IEnumerable<T> di EntityKeyMember oggetti con cui inizializzare la chiave.

Si applica a

EntityKey(String, String, Object)

Inizializza una nuova istanza della EntityKey classe con un nome del set di entità e una coppia di chiavi di entità specifica.

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)

Parametri

qualifiedEntitySetName
String

Oggetto String che rappresenta il nome del set di entità qualificato dal nome del contenitore di entità.

keyName
String

Oggetto String che rappresenta il nome della chiave.

keyValue
Object

Oggetto Object che rappresenta il valore della chiave.

Esempio

In questo esempio viene illustrato come creare e usare un oggetto 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);
    }
}

Si applica a