Udostępnij za pośrednictwem


EntityKey Konstruktory

Definicja

Inicjuje nowe wystąpienie klasy EntityKey.

Przeciążenia

EntityKey()

Inicjuje nowe wystąpienie klasy EntityKey.

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

Inicjuje EntityKey nowe wystąpienie klasy z nazwą zestawu jednostek i kolekcją ogólną KeyValuePair .

EntityKey(String, IEnumerable<EntityKeyMember>)

Inicjuje EntityKey nowe wystąpienie klasy z nazwą zestawu jednostek i kolekcją IEnumerable<T>EntityKeyMember obiektów.

EntityKey(String, String, Object)

Inicjuje EntityKey nowe wystąpienie klasy z nazwą zestawu jednostek i określoną parą kluczy jednostki.

EntityKey()

Inicjuje nowe wystąpienie klasy EntityKey.

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

Dotyczy

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

Inicjuje EntityKey nowe wystąpienie klasy z nazwą zestawu jednostek i kolekcją ogólną 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)))

Parametry

qualifiedEntitySetName
String

Jest String to nazwa zestawu jednostek kwalifikowana przez nazwę kontenera jednostki.

entityKeyValues
IEnumerable<KeyValuePair<String,Object>>

Kolekcja ogólna KeyValuePair .

Każda para klucz/wartość ma nazwę właściwości jako klucz i wartość tej właściwości jako wartość. Powinna istnieć jedna para dla każdej właściwości, która jest częścią obiektu EntityKey. Kolejność par klucz/wartość nie jest ważna, ale należy uwzględnić każdą właściwość klucza. Nazwy właściwości to proste nazwy, które nie są kwalifikowane przy użyciu nazwy typu jednostki lub nazwy schematu.

Przykłady

W tym przykładzie pokazano, jak utworzyć i użyć elementu 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);
    }
}

Dotyczy

EntityKey(String, IEnumerable<EntityKeyMember>)

Inicjuje EntityKey nowe wystąpienie klasy z nazwą zestawu jednostek i kolekcją IEnumerable<T>EntityKeyMember obiektów.

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

Parametry

qualifiedEntitySetName
String

Jest String to nazwa zestawu jednostek kwalifikowana przez nazwę kontenera jednostki.

entityKeyValues
IEnumerable<EntityKeyMember>

IEnumerable<T> Kolekcja EntityKeyMember obiektów, za pomocą których należy zainicjować klucz.

Dotyczy

EntityKey(String, String, Object)

Inicjuje EntityKey nowe wystąpienie klasy z nazwą zestawu jednostek i określoną parą kluczy jednostki.

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)

Parametry

qualifiedEntitySetName
String

Jest String to nazwa zestawu jednostek kwalifikowana przez nazwę kontenera jednostki.

keyName
String

To String jest nazwa klucza.

keyValue
Object

Wartość Object klucza.

Przykłady

W tym przykładzie pokazano, jak utworzyć i użyć elementu 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);
    }
}

Dotyczy