Тіл

EntityKey Конструкторы

Определение

Инициализирует новый экземпляр класса EntityKey.

Перегрузки

Имя Описание
EntityKey()

Инициализирует новый экземпляр класса EntityKey.

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

Инициализирует новый экземпляр EntityKey класса с именем набора сущностей и универсальной KeyValuePair коллекцией.

EntityKey(String, IEnumerable<EntityKeyMember>)

Инициализирует новый экземпляр EntityKey класса с именем набора сущностей и IEnumerable<T> коллекцией EntityKeyMember объектов.

EntityKey(String, String, Object)

Инициализирует новый экземпляр EntityKey класса с именем набора сущностей и определенной парой ключей сущностей.

EntityKey()

Инициализирует новый экземпляр класса EntityKey.

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

Применяется к

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

Инициализирует новый экземпляр EntityKey класса с именем набора сущностей и универсальной 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)))

Параметры

qualifiedEntitySetName
String

Имя String набора сущностей, заданное именем контейнера сущностей.

entityKeyValues
IEnumerable<KeyValuePair<String,Object>>

Универсальная KeyValuePair коллекция.

Каждая пара "ключ-значение" имеет имя свойства в качестве ключа и значение этого свойства в качестве значения. Для каждого свойства должна быть одна пара, которая является частью EntityKeyсвойства. Порядок пар "ключ-значение" не важен, но каждое свойство ключа должно быть включено. Имена свойств — это простые имена, которые не соответствуют имени типа сущности или имени схемы.

Примеры

В этом примере показано, как создать и использовать объект 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);
    }
}

Применяется к

EntityKey(String, IEnumerable<EntityKeyMember>)

Инициализирует новый экземпляр EntityKey класса с именем набора сущностей и IEnumerable<T> коллекцией 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))

Параметры

qualifiedEntitySetName
String

Имя String набора сущностей, заданное именем контейнера сущностей.

entityKeyValues
IEnumerable<EntityKeyMember>

Коллекция IEnumerable<T> объектов, с помощью которых инициализировать EntityKeyMember ключ.

Применяется к

EntityKey(String, String, Object)

Инициализирует новый экземпляр EntityKey класса с именем набора сущностей и определенной парой ключей сущностей.

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)

Параметры

qualifiedEntitySetName
String

Имя String набора сущностей, заданное именем контейнера сущностей.

keyName
String

Имя String ключа.

keyValue
Object

Значение Object ключа.

Примеры

В этом примере показано, как создать и использовать объект 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);
    }
}

Применяется к