Language

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の一部であるプロパティごとに 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>)

エンティティ セット名と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);
    }
}

適用対象