EntityReference<TEntity> 类

定义

表示与零或一个乘数关联的相关端。

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>
属性

示例

此示例演示如何使用该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>返回的相关对象。

方法

名称 说明
Attach(TEntity)

在对象上下文中的两个对象之间创建多对一或一对一关系。

Attach<TEntity>(IEnumerable<TEntity>, Boolean)

定义两个附加对象之间的关系。

(继承自 RelatedEnd)
CreateSourceQuery()

创建返回相关对象的等效对象查询。

Equals(Object)

确定指定的对象是否等于当前对象。

(继承自 Object)
GetEnumerator()

返回循环 IEnumerator 访问相关对象的集合。

(继承自 RelatedEnd)
GetHashCode()

用作默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
Load()

在派生类中重写时,使用默认合并选项将相关对象或对象加载到相关端。

(继承自 RelatedEnd)
Load(MergeOption)

使用指定的合并选项加载此 EntityReference<TEntity> 相关对象。

MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
Merge<TEntity>(IEnumerable<TEntity>, MergeOption, Boolean)

将相关实体合并到本地 EntityCollection<TEntity>

(继承自 RelatedEnd)
OnDeserialized(StreamingContext)

在内部用于反序列化实体对象以及 RelationshipManager 实例。

(继承自 RelatedEnd)
OnRefDeserialized(StreamingContext)

此方法在内部用于序列化相关的实体对象。

OnSerializing(StreamingContext)

此方法在内部用于序列化相关的实体对象。

ToString()

返回一个表示当前对象的字符串。

(继承自 Object)
ValidateEntityForAttach<TEntity>(TEntity, Int32, Boolean)

确定对象是否可以附加到本地集合或引用。

(继承自 RelatedEnd)
ValidateLoad<TEntity>(MergeOption, String)

确保可以将相关对象或对象成功加载到本地集合或引用中。

(继承自 RelatedEnd)
ValidateOwnerForAttach()

确保相关端所属的对象支持附加操作。

(继承自 RelatedEnd)

活动

名称 说明
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)

适用于