I m 采用一种定制的方法,追踪单个经修改的、与一层不相连的实体类别。 我从中摘取。
Programming Entity Framework: DbContext by Julia Lerman and Rowan Miller (O’Reilly). Copyright 2012 Julia Lerman and Rowan Miller, 978-1-449-31296-1.
该守则是:
public void ApplyChanges<TEntity>(TEntity root) where TEntity : class, IObjectWithState {
// bind the entity back into the context
dbContext.Set<TEntity>().Add(root);
// throw exception if entity does not implement IObjectWithState
CheckForEntitiesWithoutStateInterface(dbContext);
foreach (var entry in dbContext.ChangeTracker.Entries<IObjectWithState>()) {
IObjectWithState stateInfo = entry.Entity;
if (stateInfo.State == RecordState.Modified) {
// revert the Modified state of the entity
entry.State = EntityState.Unchanged;
foreach (var property in stateInfo.ModifiedProperties) {
// mark only the desired fields as modified
entry.Property(property).IsModified = true;
}
} else {
entry.State = ConvertState(stateInfo.State);
}
}
dbContext.SaveChanges();
}
这种方法的目的是让欧洲武装部队只了解预先界定的一组实体领域,并准备在下届“拯救儿童”呼吁中更新。 这需要这样做,才能使实体在协会工作。 NET MVC 3 如下:
on initial page load: the Get action of the controller is loading the entity object and passing it as a parameter to the view.
The View generate controls for editing 2 of the fields of the entity, and holds the ID of the record in a hidden field.
When hitting [save] and posting the entity back to the controller all of the fields excepting the 3 preserved in the view comes with a null value. This is the default behavior of the MVC binding manager.
如果在数据库中不作改动,更新的查询当然会超越未绘制地图的田地,判决如下:
UPDATE non_mapped_field_1 = NULL, ..., mapped_field_1 = mapped_value_1 , mapped_field_2 = mapped_value_2 , ... non_mapped_field_n = NULL WHERE ID = mapped_field_3
这是试图逐个跟踪领域并仅更新那些有兴趣的领域的原因。 在与ApplyChanges()交接习惯方法之前 i m 添加一意欲列入向综合国家通报的实地清单。 修改 财产清单,以便作如下陈述:
UPDATE mapped_field_1 = mapped_value_1 , mapped_field_2 = mapped_value_2 WHERE id = mapped_value_3
问题在于,在ApplyChanges修改的一个领域,即:
entry.Property(property).IsModified = true;
该系统正在扔下以下<><<> > 除外>:
{System.InvalidOperationException: Member IsModified cannot be called for property NotifyCEDeadline on entity of type User because the property is not part of the Entity Data Model.
at System.Data.Entity.Internal.InternalPropertyEntry.ValidateNotDetachedAndInModel(String method)
at System.Data.Entity.Internal.InternalPropertyEntry.set_IsModified(Boolean value)
at System.Data.Entity.Infrastructure.DbPropertyEntry.set_IsModified(Boolean value)
...
因此,问题是。 绕过这种“EF”的验证,或者让人们了解这一系统财产的存在情况,即试图改变的不动产。
arch:
- EF Code first (annotation + Fluent API)
- Oracle .NET EF Data provider (ODAC)
Context is injected to a cutom business context with nInject.MVC => this is the reason i customized the ApplyChanges() method from
using (var context = new BreakAwayContext()){ context.Set().Add(root);
简单地呼吁已经初创的目录
dbContext.Set(Root)
甲骨质数据库是人工创建的,即没有EF的帮助,因此没有使用EF元数据表。
Thanks, Ivan.