C# 如何更新在 DbContext 之外修改的实体?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12296751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 22:46:44  来源:igfitidea点击:

How to update entities which are modified outside the DbContext?

c#entity-frameworkentity-framework-4code-first

提问by Juergen Gutsch

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.

如果实体在 DbContext 之外更改(是一个分离的实体),我在更新实体时会遇到一个小问题。如果我附加修改后的实体,它的状态不会被修改。

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.

我知道 NHibernate 和它的方法 SaveOrUpdate。NHibernate 根据值决定是更新还是插入实体。

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?

使用 EF 4.x 和在 DbContext 之外修改的实体执行此操作的最佳实践是什么?我如何告诉 EF 该实体处于修改状态?

采纳答案by StuartLC

You also need to tell EF that the entity is modified, after attaching it.

您还需要在附加实体后告诉 EF 该实体已修改

context.Entry(specificationToSave).State = EntityState.Modified;

Alternatively, you can make the changes to the entity afteryou have reattached it, e.g. see MVC3 with EF 4.1 and EntityState.Modified

或者,您可以重新附加实体后对其进行更改,例如,参见MVC3 with EF 4.1 和 EntityState.Modified

Edit

编辑

You can use generics with DbSet - either class, or method - as follows:

您可以将泛型与 DbSet 一起使用 - 无论是类还是方法 - 如下:

    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.

但是,对于更高效的场景,需要我们遍历图,检测更改,然后添加新插入的、更新现有的、忽略未更改的以及从Context.

Slauma shows a great example of this here.

Slauma在这里展示了一个很好的例子。

You might want to look at using GraphDiff, which can do all this leg work for you!

您可能想考虑使用GraphDiff,它可以为您完成所有这些工作!