EntityKey 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供實體型別實例之物件的持久參考。
public ref class EntityKey sealed : IEquatable<System::Data::EntityKey ^>
[System.Runtime.Serialization.DataContract(IsReference=true)]
[System.Serializable]
public sealed class EntityKey : IEquatable<System.Data.EntityKey>
[<System.Runtime.Serialization.DataContract(IsReference=true)>]
[<System.Serializable>]
type EntityKey = class
interface IEquatable<EntityKey>
Public NotInheritable Class EntityKey
Implements IEquatable(Of EntityKey)
- 繼承
-
EntityKey
- 屬性
- 實作
範例
這些範例展示了如何建立並使用 EntityKey。
using (AdventureWorksEntities context =
new AdventureWorksEntities())
{
Object entity = null;
IEnumerable<KeyValuePair<string, object>> entityKeyValues =
new KeyValuePair<string, object>[] {
new KeyValuePair<string, object>("SalesOrderID", 43680) };
// Create the key for a specific SalesOrderHeader object.
EntityKey key = new EntityKey("AdventureWorksEntities.SalesOrderHeaders", entityKeyValues);
// Get the object from the context or the persisted store by its key.
if (context.TryGetObjectByKey(key, out entity))
{
Console.WriteLine("The requested " + entity.GetType().FullName +
" object was found");
}
else
{
Console.WriteLine("An object with this key " +
"could not be found.");
}
}
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 物件是不可變的;也就是說,建構完成後無法被修改。
建構函式
| 名稱 | Description |
|---|---|
| EntityKey() |
初始化 EntityKey 類別的新執行個體。 |
| EntityKey(String, IEnumerable<EntityKeyMember>) |
初始化一個帶有實體集合名稱及IEnumerable<T>物件集合EntityKeyMember的新類別實例EntityKey。 |
| EntityKey(String, IEnumerable<KeyValuePair<String,Object>>) |
初始化一個帶有實體集合名稱及通用KeyValuePair集合的新類別實例EntityKey。 |
| EntityKey(String, String, Object) |
初始化一個帶有實體集合名稱及特定實體鍵對的新類別實例 EntityKey 。 |
欄位
| 名稱 | Description |
|---|---|
| EntityNotValidKey | |
| NoEntitySetKey |
一個用來識別唯讀實體的單例 EntityKey 。 |
屬性
| 名稱 | Description |
|---|---|
| EntityContainerName |
取得或設定實體容器的名稱。 |
| EntityKeyValues |
取得或設定與此 EntityKey相關的關鍵值。 |
| EntitySetName |
取得或設定實體集合的名稱。 |
| IsTemporary |
會得到一個表示是否 EntityKey 暫時的值。 |
方法
| 名稱 | Description |
|---|---|
| Equals(EntityKey) |
回傳一個值,表示此實例是否等於指定的 EntityKey。 |
| Equals(Object) |
傳回值,這個值表示這個實例是否等於指定的物件。 |
| GetEntitySet(MetadataWorkspace) |
從給定的元資料工作區取得該實體金鑰的實體設定。 |
| GetHashCode() |
作為當前 EntityKey 物件的雜湊函數。 GetHashCode() 適用於雜湊演算法與資料結構,例如雜湊表。 |
| GetType() |
取得目前實例的 Type。 (繼承來源 Object) |
| MemberwiseClone() |
建立目前 Object的淺層複本。 (繼承來源 Object) |
| OnDeserialized(StreamingContext) |
輔助方法用於反序列化。EntityKey |
| OnDeserializing(StreamingContext) |
輔助方法用於反序列化。EntityKey |
| ToString() |
傳回表示目前 物件的字串。 (繼承來源 Object) |
操作員
| 名稱 | Description |
|---|---|
| Equality(EntityKey, EntityKey) |
比較兩個 EntityKey 物件。 |
| Inequality(EntityKey, EntityKey) |
比較兩個 EntityKey 物件。 |