如何更新在DbContext之外修改的实体? [英] How to update entities which are modified outside the DbContext?
问题描述
我的代码如下所示:
var specificationToSave = GetSpecificationFromTmpStore(userSessionGuid);
使用(var context = DataContextFactory.GetDataContext())
{
//这适用于更新,如果我在调试
//时修改上下文中的值,但是它会中断新实体
context.Specifications.Attach(specificationToSave);
//这适用于插入新实体,修改后的实体将被保存为新实体
context.Specifications.Add((specificationToSave);)
context.SaveChanges();
}
我知道NHibernate,它的方法SaveOrUpdate。如果NHibernate正在更新或插入实体,则会决定这些值。
使用EF 4.x和在DbContext之外修改的实体,最好的做法是什么?
我如何告诉EF这个实体处于修改状态?
你还需要告诉EF该实体在附加之后是修改。
context.Entry(specificationToSave).State = EntityState.Modified;
或者,您可以对重新附加后的实体进行更改它,例如请参阅具有EF 4.1和EntityState.Modified的MVC3
编辑
可以使用DbSet(类或方法)的泛型,如下所示:
public void Update< TEntity>(TEntity entity)
{
DbContext.Set< TEntity> ().Attach(实体);
DbContext.Entry(entity).State = EntityState.Modified;
DbContext.SaveChanges();
}
编辑:更新分离的父/子图表
为了更新效率和效能不重要的简单/浅层亲子关系,只需删除所有旧的孩子并重新插入新的孩子就很容易(虽然丑陋)解决方案但是,为了更有效的场景,我们需要遍历图形,检测更改,然后添加新插入,更新现有,忽略不变,并将删除的项从上下文
。
Slauma显示了这里的一个很好的例子。
您可能需要使用 GraphDiff ,可以为您做所有这一切工作!
I've a small problem with updating entities if the entity is changed outside the DbContext (is a detached entity). If I attach the modified entity, it's state is not modified.
My code looks like this:
var specificationToSave = GetSpecificationFromTmpStore(userSessionGuid);
using (var context = DataContextFactory.GetDataContext())
{
// this works for update, if I change the values inside the context while debugging
// but it breaks with new entities
context.Specifications.Attach(specificationToSave);
// this works for insert new entities, modified entities will be saved as new entities
context.Specifications.Add((specificationToSave);)
context.SaveChanges();
}
I know NHibernate and it's method SaveOrUpdate. NHibernate decides because of the values if it is updating or inserting the entities.
What is the best practice to do this with EF 4.x and with entities which are modified outside the DbContext? How can I tell the EF that this entity is in modified state?
You also need to tell EF that the entity is modified, after attaching it.
context.Entry(specificationToSave).State = EntityState.Modified;
Alternatively, you can make the changes to the entity after you have reattached it, e.g. see MVC3 with EF 4.1 and EntityState.Modified
Edit
You can use generics with DbSet - either class, or method - as follows:
public void Update<TEntity>(TEntity entity)
{
DbContext.Set<TEntity>().Attach(entity);
DbContext.Entry(entity).State = EntityState.Modified;
DbContext.SaveChanges();
}
Edit : For updating of detached Parent / Child Graphs
For updating of simple / shallow parent-child relationships where efficiency and performance is not important, simply deleting all old children and reinserting the new ones is an easy (although ugly) solution.
However, for a more efficient scenario requires us to traverse the graph, detect changes, and then add newly inserted, update existing, ignore unchanged, and delete removed items from the Context
.
Slauma shows a great example of this here.
You might want to look at using GraphDiff, which can do all this leg work for you!
这篇关于如何更新在DbContext之外修改的实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!