EntityKey コンストラクター

定義

EntityKey クラスの新しいインスタンスを初期化します。

オーバーロード

EntityKey()

EntityKey クラスの新しいインスタンスを初期化します。

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

エンティティ セット名およびジェネリック型の KeyValuePair コレクションを使用して、EntityKey クラスの新しいインスタンスを初期化します。

EntityKey(String, IEnumerable<EntityKeyMember>)

エンティティ セット名および EntityKeyMember オブジェクトの IEnumerable<T> コレクションを使用して、EntityKey クラスの新しいインスタンスを初期化します。

EntityKey(String, String, Object)

エンティティ セット名および特定のエンティティ キーのペアを使用して、EntityKey クラスの新しいインスタンスを初期化します。

EntityKey()

EntityKey クラスの新しいインスタンスを初期化します。

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

適用対象

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

エンティティ セット名およびジェネリック型の KeyValuePair コレクションを使用して、EntityKey クラスの新しいインスタンスを初期化します。

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 の一部であるプロパティごとに、1 つのペアが必要です。 キー/値のペアの順序は重要ではありませんが、各キー プロパティが含まれている必要があります。 プロパティ名は、エンティティ型の名前やスキーマ名で修飾されていない単純な名前です。

この例では、 を作成して使用する方法を 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>)

エンティティ セット名および EntityKeyMember オブジェクトの IEnumerable<T> コレクションを使用して、EntityKey クラスの新しいインスタンスを初期化します。

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

適用対象