MergeOption 枚举

定义

指定加载到对象上下文中的对象与对象上下文中的已有对象的合并方式。Specifies how objects being loaded into the object context are merged with objects already in the object context.

public enum class MergeOption
public enum MergeOption
type MergeOption = 
Public Enum MergeOption
继承
MergeOption

字段

AppendOnly 0

将对象上下文中不存在的对象附加到上下文。Objects that do not exist in the object context are attached to the context. 如果某个对象已存在于上下文中,则不会使用数据源值覆盖该对象的属性在对应项中的当前值和原始值。If an object is already in the context, the current and original values of object's properties in the entry are not overwritten with data source values. 该对象对应的项的状态以及该对象在对应项中的属性的状态不会更改。The state of the object's entry and state of properties of the object in the entry do not change. AppendOnly 为默认合并选项。AppendOnly is the default merge option.

NoTracking 3

对象保持为 Detached 状态,也不在 ObjectStateManager 中进行跟踪。Objects are maintained in a Detached state and are not tracked in the ObjectStateManager. 但是,Entity FrameworkEntity Framework 生成的实体和具有代理的 POCO 实体将维护对该对象上下文的引用以便于相关对象的加载。However, Entity FrameworkEntity Framework-generated entities and POCO entities with proxies maintain a reference to the object context to facilitate loading of related objects.

OverwriteChanges 1

将对象上下文中不存在的对象附加到上下文。Objects that do not exist in the object context are attached to the context. 如果某个对象已存在于上下文中,则不会使用数据源值覆盖该对象的属性在相应项中的当前值和原始值。If an object is already in the context, the current and original values of object's properties in the entry are overwritten with data source values. 该对象的对应项的状态设置为 Unchanged,并且不会将任何属性标记为已修改。The state of the object's entry is set to Unchanged, no properties are marked as modified.

PreserveChanges 2

将对象上下文中不存在的对象附加到上下文。Objects that do not exist in the object context are attached to the context.

注解

实体框架只维护缓存中具有特定实体键的对象的单个实例。The Entity Framework only maintains a single instance of an object with a specific entity key in the cache. EntityKey对象是不可变对象, 表示对象的标识。The EntityKey objects are immutable objects that represent object's identity. 实体键用于在对象上下文中执行标识解析。Entity keys are used to perform identity resolution in the object context. 有关详细信息, 请参阅使用实体键For more information, see Working with Entity Keys. 如果已跟踪具有相同标识的实体, 则将根据MergeOption查询的合并来自数据源的数据和已在状态管理器中的数据。If an entity with the same identity is already being tracked, the data coming from the data source and the data already in the state manager are merged according to the MergeOption of the query.

有关的其他信息MergeOption.PreserveChangesAdditional information on MergeOption.PreserveChanges

如果该实体的状态为 EntityState.Unchanged,则会使用数据源值覆盖对应项中的当前值和原始值。If the state of the entity is EntityState.Unchanged, the current and original values in the entry are overwritten with data source values. 该实体的状态保持为 EntityState.Unchanged,并且不会将任何属性标记为已修改。The state of the entity remains EntityState.Unchanged and no properties are marked as modified.

如果该实体的状态为 EntityState.Modified,则不会使用数据源值覆盖已修改属性的当前值。If the state of the entity is EntityState.Modified, the current values of modified properties are not overwritten with data source values. 将使用数据源中的值覆盖未修改属性的原始值。The original values of unmodified properties are overwritten with the values from the data source.

在 .NET Framework 4 中, 实体框架将未修改属性的当前值与从数据源返回的值进行比较。In .NET Framework 4, the Entity Framework compares the current values of unmodified properties with the values that were returned from the data source. 如果值不相同,则将该属性标记为已修改。If the values are not the same, the property is marked as modified.

在 .NET Framework 3.5 SP1 中, 实体框架不会将属性标记为已修改, 即使数据源中的值不同也是如此。In .NET Framework 3.5 SP1, the Entity Framework does not mark the property as modified even if the value in the data source is different.

调用 ObjectContext.SaveChanges 时,只会将已修改的属性保存到数据源中。Only modified properties are persisted to the data source when you call ObjectContext.SaveChanges.

若要保留 .NET Framework 3.5 SP1 行为, 请ObjectContextOptions.UseLegacyPreserveChangesBehaviortrue设置为。To preserve the .NET Framework 3.5 SP1 behavior, set ObjectContextOptions.UseLegacyPreserveChangesBehavior to true. 在本地上下文中保留更改时,可以使用 PreserveChanges 解决开放式并发异常。The PreserveChanges option can be used to resolve optimistic concurrency exceptions while preserving changes in the local context. 有关详细信息, 请参阅保存更改和管理并发For more information, see Saving Changes and Managing Concurrency.

适用于