实体框架未更新现有相关实体 [英] Entity Framework not updating existing related entity

查看:114
本文介绍了实体框架未更新现有相关实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个广泛使用Entity Framework的项目.而且一切正常.

I have a project which uses Entity Framework extensively. And it all works fine.

但是,我正在尝试进行一次更新,但不会更新,也不会引发错误.

However, there is one update that I am trying to make and it does not update, nor is an error thrown.

基本过程是,我采用一个现有实体,并与之一起创建一个新的(和不同类型的)实体.新实体保存得很好,但现有实体却不能.

The basic process is that I take an existing entity, work with it to create a new (and different type) of entity. The new entity saves just fine, but the existing one does not.

通常,当我遇到此问题时,这意味着该实体未附加到上下文.但是,确实如此,我尝试再次附加它并添加"它,但是我没有运气.

Typically when I have encountered this, it means that the entity is not attached to the context. However, it is, and I have tried attaching it again as well as "Adding" it, but I have had no luck.

        if (fileRowEntity != null)
        {
            this.Context.FileRowEntities.Attach(fileRowEntity);
            fileRowEntity.FileRowStatusId = (int)FileRowStatus.Converted;
            fileRowEntity.EdiDocument = ediDocument;
        }

        ediDocument.InsertedDate = DateTime.Now;
        ediDocument.EdiDocumentGuid = Guid.NewGuid();
        ediDocument.DocumentMetatdata = null;
        this.Context.EdiDocuments.Add(ediDocument);
        var count = this.Context.SaveChanges();

ediDocument已保存,但fileRowEntity未保存.

我正在拔头发试图解决这个问题.我已经尝试在fileRowEntity上进行第二次显式保存,但返回时保存的更改为零:

I am tearing my hair out trying to figure this out. I have tried a second explicit save on the fileRowEntity but it comes back with zero changes saved:

        if (fileRowEntity != null)
        {
            fileRowEntity.EdiDocumentId = ediDocument.EdiDocumentId;
            fileRowEntity.Column100 = "X";
            this.Context.FileRowEntities.Attach(fileRowEntity);
            count = this.Context.SaveChanges();
        }

count始终为零,并且不会更新数据库.

count is always zero, and the database is not updated.

我不知道还有什么可以尝试调试的.

I don't know what else to try to debug this.

推荐答案

将实体附加到上下文不会将其标记为已修改.

Attaching an entity to a context does not mark it as modified.

在您的情况下,假定您正在调用SaveChanges()的上下文实例与检索fileRowEntity对象的对象不同,因此它不知道它已被修改(或被修改了什么) ).

In your case, the presumption is that the context instance where you are calling SaveChanges() on, is not the same that retrieved the fileRowEntity object, so it doesn't know that it was modified (or what was modified).

DbContext从商店中检索对象时,它将存储其原始值的副本(除非您在该查询中使用了AsNoTracking()),并且每当它通过调用DetectChanges()来检测到更改时,可以显式地创建,但是普通的DbContext实现会在很多地方单独调用它),它将存储新的对象值.如果两者之间存在差异,则对SaveChanges的下一次调用将更新商店/数据库中的实体.

When a DbContext retrieves an object from the store, it stores a copy of its original values (unless you used AsNoTracking() on that query), and whenever it detects changes by a call to DetectChanges(), which you can make explicitly, but the normal DbContext implementation will call it by itself at many points), it'll store the new object values. If there are differences between those, the next call to SaveChanges will update the entity in the store/database.

当您附加实体但未将其标记为已修改时,它不知道任何更改,因此您可以将实体显式标记为已修改:

When you attach an entity but don't mark it as modified, it doesn't know anything has changed, so you can explicitly mark the entity as modified:

使用Context.Entry(fileRowEntity).State = EntityState.Modified;应该告诉EF您的实体已被修改,并且当您为整个实体调用SaveChanges()时,它将生成更新命令(它将在UPDATE SQL中发送所有字段值) .请注意,如果未将实体附加到上下文,则执行此操作还将附加该实体(无需附加该实体,然后设置其状态).

Using Context.Entry(fileRowEntity).State = EntityState.Modified; should tell EF that your entity was modified, and it'll generate the update command when you call SaveChanges() for the whole entity (it'll send all the field values in the UPDATE SQL). Note that doing this also attaches the entity if it was not attached to the context (no need to attach it and then set its state).

您还可以仅标记已修改的属性(或更改实体OriginalValues),这样可以优化查询(它只会更新实际更改的字段).自己跟踪这些更改比较麻烦,但是如果需要进行额外的优化,则值得一试(个人而言,除非数据库服务器上有关键的负载并且每一位都很重要,我都不会这样做,但您可以选择)

You can also mark only the properties that were modified (or change the entity OriginalValues), this way your query will be optimized (it'll only update the fields that have actually changed). It's a bit cumbersome to track those changes yourself, but if you need that extra optimization, it's worth a shot (personally, unless there's a critical load on the database server and every bit counts, I wouldn't do this, but your choice)

这篇关于实体框架未更新现有相关实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆