通过


ObjectStateManager 类

定义

维护实体类型实例和关系实例的对象状态和标识管理。

public ref class ObjectStateManager
public class ObjectStateManager
type ObjectStateManager = class
Public Class ObjectStateManager
继承
ObjectStateManager

示例

以下示例从ObjectContext上下文中获取ObjectStateManager并使用状态管理器访问对象。

int orderId = 43680;

using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    ObjectStateManager objectStateManager = context.ObjectStateManager;
    ObjectStateEntry stateEntry = null;

    var order = (from o in context.SalesOrderHeaders
                 where o.SalesOrderID == orderId
                 select o).First();

    // Attempts to retrieve ObjectStateEntry for the given EntityKey.
    bool isPresent = objectStateManager.TryGetObjectStateEntry(((IEntityWithKey)order).EntityKey, out stateEntry);
    if (isPresent)
    {
        Console.WriteLine("The entity was found");
    }
}

下面的示例使用 TryGetObjectStateEntry(EntityKey, ObjectStateEntry) 返回 ObjectStateManager 的方法根据其实体键获取对象。

private static void ApplyItemUpdates(SalesOrderDetail originalItem,
    SalesOrderDetail updatedItem)
{
    using (AdventureWorksEntities context =
        new AdventureWorksEntities())
    {
        context.SalesOrderDetails.Attach(updatedItem);
        // Check if the ID is 0, if it is the item is new.
        // In this case we need to chage the state to Added.
        if (updatedItem.SalesOrderDetailID == 0)
        {
            // Because the ID is generated by the database we do not need to
            // set updatedItem.SalesOrderDetailID.
            context.ObjectStateManager.ChangeObjectState(updatedItem, System.Data.EntityState.Added);
        }
        else
        {
            // If the SalesOrderDetailID is not 0, then the item is not new
            // and needs to be updated. Because we already added the
            // updated object to the context we need to apply the original values.
            // If we attached originalItem to the context
            // we would need to apply the current values:
            // context.ApplyCurrentValues("SalesOrderDetails", updatedItem);
            // Applying current or original values, changes the state
            // of the attached object to Modified.
            context.ApplyOriginalValues("SalesOrderDetails", originalItem);
        }
        context.SaveChanges();
    }
}

注解

ObjectStateManager 跟踪查询结果,并提供用于合并多个重叠查询结果的逻辑。 当用户插入、删除或修改对象时,它还会执行内存中更改跟踪,并提供更新的更改集。 更改处理器使用此更改集来保留修改。

此类通常用于应用程序中,而不是直接在应用程序中使用 ObjectContext

构造函数

名称 说明
ObjectStateManager(MetadataWorkspace)

初始化 ObjectStateManager 类的新实例。

属性

名称 说明
MetadataWorkspace

获取 MetadataWorkspace 与此状态管理器关联的值。

方法

名称 说明
ChangeObjectState(Object, EntityState)

ObjectStateEntry 特定对象的状态更改为指定的 entityState

ChangeRelationshipState(Object, Object, String, EntityState)

更改根据两个相关对象和导航属性的名称指定的两个实体对象之间的关系的状态。

ChangeRelationshipState(Object, Object, String, String, EntityState)

更改基于两个相关对象和关系属性指定的两个实体对象之间的关系的状态。

ChangeRelationshipState<TEntity>(TEntity, Object, Expression<Func<TEntity,Object>>, EntityState)

更改基于两个相关对象和定义导航属性的 LINQ 表达式指定的两个实体对象之间的关系的状态。

Equals(Object)

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

(继承自 Object)
GetHashCode()

用作默认哈希函数。

(继承自 Object)
GetObjectStateEntries(EntityState)

返回对象或具有给定状态的关系的对象集合 ObjectStateEntry

GetObjectStateEntry(EntityKey)

返回具有指定键的对象或关系项的一个 ObjectStateEntry

GetObjectStateEntry(Object)

返回指定对象的一个 ObjectStateEntry

GetRelationshipManager(Object)

返回 RelationshipManager 指定对象使用的对象。

GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
ToString()

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

(继承自 Object)
TryGetObjectStateEntry(EntityKey, ObjectStateEntry)

尝试检索与指定EntityKey对象或关系对应的ObjectStateEntry对象或关系。

TryGetObjectStateEntry(Object, ObjectStateEntry)

尝试检索指定Object项的对应ObjectStateEntry项。

TryGetRelationshipManager(Object, RelationshipManager)

返回 RelationshipManager 指定对象使用的对象。

活动

名称 说明
ObjectStateManagerChanged

在向状态管理器添加或删除实体时发生。

适用于