EntityKey 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 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(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)))
參數
- 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))
參數
- 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)
參數
範例
此範例示範如何建立和使用 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);
}
}