EntityKey Konstruktorer

Definition

Initierar en ny instans av EntityKey klassen.

Överlagringar

Name Description
EntityKey()

Initierar en ny instans av EntityKey klassen.

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

Initierar en ny instans av EntityKey klassen med ett entitetsuppsättningsnamn och en allmän KeyValuePair samling.

EntityKey(String, IEnumerable<EntityKeyMember>)

Initierar en ny instans av EntityKey klassen med ett entitetsuppsättningsnamn och en IEnumerable<T> samling EntityKeyMember objekt.

EntityKey(String, String, Object)

Initierar en ny instans av EntityKey klassen med ett entitetsuppsättningsnamn och ett specifikt entitetsnyckelpar.

EntityKey()

Initierar en ny instans av EntityKey klassen.

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

Gäller för

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

Initierar en ny instans av EntityKey klassen med ett entitetsuppsättningsnamn och en allmän KeyValuePair samling.

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

Parametrar

qualifiedEntitySetName
String

A String som är namnet på entitetsuppsättningen som kvalificerats av entitetens containernamn.

entityKeyValues
IEnumerable<KeyValuePair<String,Object>>

En allmän KeyValuePair samling.

Varje nyckel/värde-par har ett egenskapsnamn som nyckel och värdet för egenskapen som värde. Det bör finnas ett par för varje egenskap som är en del av EntityKey. Ordningen på nyckel/värde-paren är inte viktig, men varje nyckelegenskap bör inkluderas. Egenskapsnamnen är enkla namn som inte är kvalificerade med ett entitetstypnamn eller schemanamnet.

Exempel

Det här exemplet visar hur du skapar och använder en 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);
    }
}

Gäller för

EntityKey(String, IEnumerable<EntityKeyMember>)

Initierar en ny instans av EntityKey klassen med ett entitetsuppsättningsnamn och en IEnumerable<T> samling EntityKeyMember objekt.

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

Parametrar

qualifiedEntitySetName
String

A String som är namnet på entitetsuppsättningen som kvalificerats av entitetens containernamn.

entityKeyValues
IEnumerable<EntityKeyMember>

En IEnumerable<T> samling EntityKeyMember objekt som nyckeln ska initieras med.

Gäller för

EntityKey(String, String, Object)

Initierar en ny instans av EntityKey klassen med ett entitetsuppsättningsnamn och ett specifikt entitetsnyckelpar.

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)

Parametrar

qualifiedEntitySetName
String

A String som är namnet på entitetsuppsättningen som kvalificerats av entitetens containernamn.

keyName
String

Ett String som är namnet på nyckeln.

keyValue
Object

Ett Object som är nyckelvärdet.

Exempel

Det här exemplet visar hur du skapar och använder en 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);
    }
}

Gäller för