EntityReference<TEntity> 类
定义
表示重数为零或一的关联的相关端。Represents a related end of an association with a multiplicity of zero or one.
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
引用的实体类型。The entity type of the reference.
- 继承
- 属性
示例
此示例演示如何使用 EntityReference<TEntity> 对象更改 SalesOrderHeader 对象和表示订单发货地址的相关 Address 对象之间的关系。This example shows how to use the EntityReference<TEntity> object to change a relationship between a SalesOrderHeader object and a related Address object that represents the shipping address for the order.
// 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);
}
' Define the order and new address IDs.
Dim orderId As Integer = 43669
Dim addressId As Integer = 26
Using context As New AdventureWorksEntities()
' Get the billing address to change to.
Dim address As Address = context.Addresses.Single(Function(c) c.AddressID = addressId)
' Get the order being changed.
Dim order As SalesOrderHeader = context.SalesOrderHeaders.Single(Function(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 Not order.Address.Equals(address) Then
' Use Address navigation property to change the association.
order.Address = address
' Write the changed billing street address.
Console.WriteLine("Changed street: " & order.Address.AddressLine1)
End If
' If the address change succeeds, save the changes.
context.SaveChanges()
' Write the current billing street address.
Console.WriteLine("Current street: " & order.Address.AddressLine1)
End Using
注解
当相关端的重数为零或一时,导航属性返回一个 EntityReference<TEntity> 对象。An EntityReference<TEntity> object is returned by a navigation property when the related end has a multiplicity of zero or one. 有关详细信息,请参阅 NavigationProperty 元素 (CSDL) 。For more information, see NavigationProperty Element (CSDL).
EntityReference<TEntity> 对象可能在关系的另一端具有相应的 EntityCollection<TEntity>(一对多关系)或 EntityReference<TEntity>(一对一关系)。An EntityReference<TEntity> object might have a corresponding EntityCollection<TEntity> (one-to-many relationship) or EntityReference<TEntity> (one-to-one relationship) at the other end of the relationship. 当 EntityReference<TEntity> 和 EntityCollection<TEntity> 模型位于同一关系的相反端时,该关系的完整性维持在对象级别。When an EntityReference<TEntity> and an EntityCollection<TEntity> model opposing ends of the same relationship, the integrity of the relationship is maintained at the object level.
此类不能被继承。This class cannot be inherited.
构造函数
| EntityReference<TEntity>() |
创建 EntityReference<TEntity> 的新实例。Creates a new instance of EntityReference<TEntity>. |
属性
| EntityKey |
返回相关对象的键。Returns the key for the related object. (继承自 EntityReference) |
| IsLoaded |
获取一个值,该值指示是否已加载所有相关对象。Gets a value that indicates whether all related objects have been loaded. (继承自 RelatedEnd) |
| RelationshipName |
获取此相关端所参与的关系的名称。Gets the name of the relationship in which this related end participates. (继承自 RelatedEnd) |
| RelationshipSet |
获取对相关端元数据的引用。Gets a reference to the metadata for the related end. (继承自 RelatedEnd) |
| SourceRoleName |
获取关系的源端的角色名称。Gets the role name at the source end of the relationship. (继承自 RelatedEnd) |
| TargetRoleName |
获取关系目标端处的角色名称。Gets the role name at the target end of the relationship. (继承自 RelatedEnd) |
| Value |
获取或设置此 EntityReference<TEntity> 返回的相关对象。Gets or sets the related object returned by this EntityReference<TEntity>. |
方法
| Attach(TEntity) |
在对象上下文中创建两个对象之间的多对一或一对一关系。Creates a many-to-one or one-to-one relationship between two objects in the object context. |
| Attach<TEntity>(IEnumerable<TEntity>, Boolean) |
定义两个附加对象之间的关系。Defines a relationship between two attached objects. (继承自 RelatedEnd) |
| CreateSourceQuery() |
创建一个返回相关对象的等效对象查询。Creates an equivalent object query that returns the related object. |
| Equals(Object) |
确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object. (继承自 Object) |
| GetEnumerator() |
返回一个 IEnumerator,它循环访问相关对象的集合。Returns an IEnumerator that iterates through the collection of related objects. (继承自 RelatedEnd) |
| GetHashCode() |
作为默认哈希函数。Serves as the default hash function. (继承自 Object) |
| GetType() |
获取当前实例的 Type。Gets the Type of the current instance. (继承自 Object) |
| Load() |
在派生类中重写时,使用默认合并选项将一个或多个相关对象加载到相关端中。When overridden in a derived class, loads the related object or objects into the related end with the default merge option. (继承自 RelatedEnd) |
| Load(MergeOption) |
使用指定的合并选项为此 EntityReference<TEntity> 加载相关对象。Loads the related object for this EntityReference<TEntity> with the specified merge option. |
| MemberwiseClone() |
创建当前 Object 的浅表副本。Creates a shallow copy of the current Object. (继承自 Object) |
| Merge<TEntity>(IEnumerable<TEntity>, MergeOption, Boolean) |
将相关实体合并到本地 EntityCollection<TEntity> 中。Merges related entities into the local EntityCollection<TEntity>. (继承自 RelatedEnd) |
| OnDeserialized(StreamingContext) |
在内部使用,用于反序列化实体对象与 RelationshipManager 实例。Used internally to deserialize entity objects along with the RelationshipManager instances. (继承自 RelatedEnd) |
| OnRefDeserialized(StreamingContext) |
此方法在内部使用,用于序列化相关实体对象。This method is used internally to serialize related entity objects. |
| OnSerializing(StreamingContext) |
此方法在内部使用,用于序列化相关实体对象。This method is used internally to serialize related entity objects. |
| ToString() |
返回表示当前对象的字符串。Returns a string that represents the current object. (继承自 Object) |
| ValidateEntityForAttach<TEntity>(TEntity, Int32, Boolean) |
确定对象可以附加到本地集合还是引用。Determines whether an object can be attached to the local collection or reference. (继承自 RelatedEnd) |
| ValidateLoad<TEntity>(MergeOption, String) |
确保相关对象可以成功加载到本地集合或引用中。Ensures that the related object or objects can be successfully loaded into the local collection or reference. (继承自 RelatedEnd) |
| ValidateOwnerForAttach() |
确保相关端所属的对象支持附加操作。Ensures that the object to which the related end belongs supports an attach operation. (继承自 RelatedEnd) |
事件
| AssociationChanged |
对相关端进行更改时发生。Occurs when a change is made to a related end. (继承自 RelatedEnd) |
显式接口实现
| IRelatedEnd.Add(IEntityWithRelationships) |
将对象添加到相关端。Adds an object to the related end. (继承自 RelatedEnd) |
| IRelatedEnd.Add(Object) |
将对象添加到相关端。Adds an object to the related end. (继承自 RelatedEnd) |
| IRelatedEnd.Attach(IEntityWithRelationships) |
将对象附加到相关端。Attaches an object at the related end. (继承自 RelatedEnd) |
| IRelatedEnd.Attach(Object) |
将对象附加到相关端。Attaches an object to the related end. (继承自 RelatedEnd) |
| IRelatedEnd.CreateSourceQuery() |
返回一个 IEnumerable,它表示属于相关端的对象。Returns an IEnumerable that represents the objects that belong to the related end. (继承自 RelatedEnd) |
| IRelatedEnd.Remove(IEntityWithRelationships) |
从相关端的集合中移除一个对象。Removes an object from the collection at the related end. (继承自 RelatedEnd) |
| IRelatedEnd.Remove(Object) |
如果某个对象是相关端的一部分,则从该相关端中移除该对象。Removes an object from the related end if the object is part of the related end. (继承自 RelatedEnd) |