EntityReference<TEntity> 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
代表關聯的相關端點,重數為零或一。
generic <typename TEntity>
where TEntity : class, IEntityWithRelationshipspublic ref class EntityReference sealed : System::Data::Objects::DataClasses::EntityReference
generic <typename TEntity>
where TEntity : classpublic ref class EntityReference sealed : System::Data::Objects::DataClasses::EntityReference
[System.Runtime.Serialization.DataContract]
[System.Serializable]
public sealed class EntityReference<TEntity> : System.Data.Objects.DataClasses.EntityReference where TEntity : class, IEntityWithRelationships
[System.Runtime.Serialization.DataContract]
[System.Serializable]
public sealed class EntityReference<TEntity> : System.Data.Objects.DataClasses.EntityReference where TEntity : class
[<System.Runtime.Serialization.DataContract>]
[<System.Serializable>]
type EntityReference<'Entity (requires 'Entity : null and 'Entity :> IEntityWithRelationships)> = class
inherit EntityReference
[<System.Runtime.Serialization.DataContract>]
[<System.Serializable>]
type EntityReference<'Entity (requires 'Entity : null)> = class
inherit EntityReference
Public NotInheritable Class EntityReference(Of TEntity)
Inherits EntityReference
類型參數
- TEntity
參考的實體類型。
- 繼承
- 屬性
範例
此範例展示了如何利用該EntityReference<TEntity>物件改變物件與代表訂單運送地址的相關Address物件之間的SalesOrderHeader關係。
// Define the order and new address IDs.
int orderId = 43669;
int addressId = 26;
using (AdventureWorksEntities context
= new AdventureWorksEntities())
{
// Get the billing address to change to.
Address address =
context.Addresses.Single(c => c.AddressID == addressId);
// Get the order being changed.
SalesOrderHeader order =
context.SalesOrderHeaders.Single(o => o.SalesOrderID == orderId);
// You do not have to call the Load method to load the addresses for the order,
// because lazy loading is set to true
// by the constructor of the AdventureWorksEntities object.
// With lazy loading set to true the related objects are loaded when
// you access the navigation property. In this case Address.
// Write the current billing street address.
Console.WriteLine("Current street: "
+ order.Address.AddressLine1);
// Change the billing address.
if (!order.Address.Equals(address))
{
// Use Address navigation property to change the association.
order.Address = address;
// Write the changed billing street address.
Console.WriteLine("Changed street: "
+ order.Address.AddressLine1);
}
// If the address change succeeds, save the changes.
context.SaveChanges();
// Write the current billing street address.
Console.WriteLine("Current street: "
+ order.Address.AddressLine1);
}
備註
EntityReference<TEntity>當相關端點的重數為零或一時,該物件由導航性質回傳。 欲了解更多資訊,請參閱 NavigationProperty Element(CSDL)。
一個 EntityReference<TEntity> 物件可能在關係的另一端有對應 EntityCollection<TEntity> 的(一對多關係)或 EntityReference<TEntity> (一對一的關係)。 當 與 EntityReference<TEntity>EntityCollection<TEntity> 模型處於同一關係的對立端點時,該關係的完整性在物件層級得以維持。
此類別無法獲得繼承。
建構函式
| 名稱 | Description |
|---|---|
| EntityReference<TEntity>() |
建立一個新的 實例。EntityReference<TEntity> |
屬性
| 名稱 | Description |
|---|---|
| EntityKey |
回傳相關物件的金鑰。 (繼承來源 EntityReference) |
| IsLoaded |
會得到一個值,表示所有相關物件是否都已載入。 (繼承來源 RelatedEnd) |
| RelationshipName |
取得該相關端參與的關係名稱。 (繼承來源 RelatedEnd) |
| RelationshipSet |
會取得相關端的元資料參考。 (繼承來源 RelatedEnd) |
| SourceRoleName |
在關係的原始端取得角色名稱。 (繼承來源 RelatedEnd) |
| TargetRoleName |
在關係的目標端取得角色名稱。 (繼承來源 RelatedEnd) |
| Value |
取得或設定由此 EntityReference<TEntity>回傳的相關物件。 |
方法
事件
| 名稱 | Description |
|---|---|
| AssociationChanged |
當變更發生在相關目的時。 (繼承來源 RelatedEnd) |
明確介面實作
| 名稱 | Description |
|---|---|
| IRelatedEnd.Add(IEntityWithRelationships) |
在相關端加上一個物件。 (繼承來源 RelatedEnd) |
| IRelatedEnd.Add(Object) |
在相關端加上一個物件。 (繼承來源 RelatedEnd) |
| IRelatedEnd.Attach(IEntityWithRelationships) |
在相關端連接一個物體。 (繼承來源 RelatedEnd) |
| IRelatedEnd.Attach(Object) |
將物件附著於相關端。 (繼承來源 RelatedEnd) |
| IRelatedEnd.CreateSourceQuery() |
回傳 和 IEnumerable ,代表屬於相關端點的物件。 (繼承來源 RelatedEnd) |
| IRelatedEnd.Remove(IEntityWithRelationships) |
從相關集合端移除一個物件。 (繼承來源 RelatedEnd) |
| IRelatedEnd.Remove(Object) |
若物件是相關端的一部分,則該物件會從相關端移除。 (繼承來源 RelatedEnd) |