EntityKey 建構函式

定義

初始化 EntityKey 類別的新執行個體。

多載

名稱 Description
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

A 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

A 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

A String 是實體集合名稱,並以實體容器名稱作為限定。

keyName
String

A 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);
    }
}

適用於