EntityKey Konstruktorok

Definíció

Inicializálja a EntityKey osztály új példányát.

Túlterhelések

Name Description
EntityKey()

Inicializálja a EntityKey osztály új példányát.

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

Inicializálja az EntityKey osztály új példányát egy entitáskészlet nevével és egy általános KeyValuePair gyűjteménysel.

EntityKey(String, IEnumerable<EntityKeyMember>)

Inicializálja az EntityKey osztály új példányát egy entitáskészlet nevével és egy IEnumerable<T> objektumgyűjteménysel EntityKeyMember .

EntityKey(String, String, Object)

Inicializálja az EntityKey osztály új példányát egy entitáskészlet nevével és egy adott entitáskulcs-párral.

EntityKey()

Inicializálja a EntityKey osztály új példányát.

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

A következőre érvényes:

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

Inicializálja az EntityKey osztály új példányát egy entitáskészlet nevével és egy általános KeyValuePair gyűjteménysel.

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

Paraméterek

qualifiedEntitySetName
String

Az String entitástároló neve által minősített entitáskészlet neve.

entityKeyValues
IEnumerable<KeyValuePair<String,Object>>

Általános KeyValuePair gyűjtemény.

Minden kulcs/érték párnak van egy tulajdonságneve kulcsként, és a tulajdonság értéke értékként. Minden tulajdonsághoz egy párnak kell lennie, amely a EntityKey. A kulcs/érték párok sorrendje nem fontos, de minden kulcstulajdonságnak szerepelnie kell. A tulajdonságnevek olyan egyszerű nevek, amelyek nem rendelkeznek entitástípusnévvel vagy sémanévvel.

Példák

Ez a példa bemutatja, hogyan hozhat létre és használhat egy 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);
    }
}

A következőre érvényes:

EntityKey(String, IEnumerable<EntityKeyMember>)

Inicializálja az EntityKey osztály új példányát egy entitáskészlet nevével és egy IEnumerable<T> objektumgyűjteménysel EntityKeyMember .

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

Paraméterek

qualifiedEntitySetName
String

Az String entitástároló neve által minősített entitáskészlet neve.

entityKeyValues
IEnumerable<EntityKeyMember>

Objektumgyűjtemény IEnumerable<T>EntityKeyMember , amellyel inicializálni szeretné a kulcsot.

A következőre érvényes:

EntityKey(String, String, Object)

Inicializálja az EntityKey osztály új példányát egy entitáskészlet nevével és egy adott entitáskulcs-párral.

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)

Paraméterek

qualifiedEntitySetName
String

Az String entitástároló neve által minősített entitáskészlet neve.

keyName
String

Ez String a kulcs neve.

keyValue
Object

Ez Object a kulcsérték.

Példák

Ez a példa bemutatja, hogyan hozhat létre és használhat egy 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);
    }
}

A következőre érvényes: