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);
}
注解
当相关端的乘数为零或 1 时,导航属性将返回对象 EntityReference<TEntity> 。 有关详细信息,请参阅 NavigationProperty 元素(CSDL)。
对象 EntityReference<TEntity> 在关系的另一端可能有相应的 EntityCollection<TEntity> (一对多关系)或 EntityReference<TEntity> (一对一关系)。 EntityReference<TEntity>当同一关系的一个和一个EntityCollection<TEntity>模型对立端时,关系的完整性将保留在对象级别。
此类不能被继承。
构造函数
| 名称 | 说明 |
|---|---|
| EntityReference<TEntity>() |
创建 EntityReference<TEntity>的新实例。 |
属性
| 名称 | 说明 |
|---|---|
| EntityKey |
返回相关对象的键。 (继承自 EntityReference) |
| IsLoaded |
获取一个值,该值指示是否已加载所有相关对象。 (继承自 RelatedEnd) |
| RelationshipName |
获取此相关端参与的关系的名称。 (继承自 RelatedEnd) |
| RelationshipSet |
获取对相关端元数据的引用。 (继承自 RelatedEnd) |
| SourceRoleName |
获取关系源端的角色名称。 (继承自 RelatedEnd) |
| TargetRoleName |
获取关系目标端的角色名称。 (继承自 RelatedEnd) |
| Value |
获取或设置由此 EntityReference<TEntity>返回的相关对象。 |
方法
活动
| 名称 | 说明 |
|---|---|
| AssociationChanged |
在对相关端进行更改时发生。 (继承自 RelatedEnd) |
显式接口实现
| 名称 | 说明 |
|---|---|
| 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) |