EntityKey Konstruktoren

Definition

Initialisiert eine neue Instanz der EntityKey-Klasse.

Überlädt

Name Beschreibung
EntityKey()

Initialisiert eine neue Instanz der EntityKey-Klasse.

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

Initialisiert eine neue Instanz der EntityKey Klasse mit einem Entitätssatznamen und einer generischen KeyValuePair Auflistung.

EntityKey(String, IEnumerable<EntityKeyMember>)

Initialisiert eine neue Instanz der EntityKey Klasse mit einem Entitätssatznamen und einer IEnumerable<T> Auflistung von EntityKeyMember Objekten.

EntityKey(String, String, Object)

Initialisiert eine neue Instanz der EntityKey Klasse mit einem Entitätssatznamen und einem bestimmten Entitätsschlüsselpaar.

EntityKey()

Initialisiert eine neue Instanz der EntityKey-Klasse.

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

Gilt für:

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

Initialisiert eine neue Instanz der EntityKey Klasse mit einem Entitätssatznamen und einer generischen KeyValuePair Auflistung.

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

Parameter

qualifiedEntitySetName
String

A String that is the entity set name qualified by the entity container name.

entityKeyValues
IEnumerable<KeyValuePair<String,Object>>

Eine generische KeyValuePair Auflistung.

Jedes Schlüssel-Wert-Paar weist einen Eigenschaftsnamen als Schlüssel und den Wert dieser Eigenschaft als Wert auf. Es sollte ein Paar für jede Eigenschaft vorhanden sein, die Teil der EntityKey. Die Reihenfolge der Schlüssel-Wert-Paare ist nicht wichtig, aber jede Schlüsseleigenschaft sollte einbezogen werden. Die Eigenschaftennamen sind einfache Namen, die nicht mit einem Entitätstypnamen oder dem Schemanamen qualifiziert sind.

Beispiele

In diesem Beispiel wird gezeigt, wie Sie eine 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);
    }
}

Gilt für:

EntityKey(String, IEnumerable<EntityKeyMember>)

Initialisiert eine neue Instanz der EntityKey Klasse mit einem Entitätssatznamen und einer IEnumerable<T> Auflistung von EntityKeyMember Objekten.

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

Parameter

qualifiedEntitySetName
String

A String that is the entity set name qualified by the entity container name.

entityKeyValues
IEnumerable<EntityKeyMember>

Eine IEnumerable<T> Auflistung von EntityKeyMember Objekten, mit denen der Schlüssel initialisiert werden soll.

Gilt für:

EntityKey(String, String, Object)

Initialisiert eine neue Instanz der EntityKey Klasse mit einem Entitätssatznamen und einem bestimmten Entitätsschlüsselpaar.

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)

Parameter

qualifiedEntitySetName
String

A String that is the entity set name qualified by the entity container name.

keyName
String

A String that is the name of the key.

keyValue
Object

Ein Object Schlüsselwert.

Beispiele

In diesem Beispiel wird gezeigt, wie Sie eine 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);
    }
}

Gilt für: