当使用EF连接,是可以不覆盖我还没有更新的属性? [英] When using Attach in EF, is it possible to not overwrite properties I haven't Updated?

查看:160
本文介绍了当使用EF连接,是可以不覆盖我还没有更新的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被附加到我的数据上下文这样更新现有的实体:

I'm updating an existing entity by attaching it to my data context like this:

    var updatedDocumentState = new AccDocumentState()
    {
        Id = accDocumentState.Id,
        IsDocumentary = accDocumentState.IsDocumentary,
        IsEditable = accDocumentState.IsEditable,
        IsRecursive = accDocumentState.IsRecursive,
        Title = accDocumentState.Title,
       Reportable = accDocumentState.Reportable,

    };
        context.AccDocumentStates.Attach(updatedDocumentState);
        context.ObjectStateManager.ChangeObjectState(updatedDocumentState, System.Data.EntityState.Modified);
        flag = context.SaveChanges() > 0;

和这个作品,但是节省了连接实体后,我没现有实体的属性 T更新的,但我想保留,因为他们,都将被覆盖并给予空值。我怎么能把我的实体,并保持我还没有更新现有实体的属性?

And this works, however after saving the attached entity, the properties of the existing entity which i didn't update, but i want to keep as they were, are overwritten and given null values. How can I attach my entity and keep the properties of the existing entity which i have not updated?

推荐答案

EF有一个对象数据更改跟踪。通过代理
跟踪启用在波科项更改

EF has an Object Data change tracker. Is enabled via proxies Tracking changes in Poco entries

基本上你/找到第一读取对象/波科实体。
变动只是想让这些属性。和保存。
只有更改的属性将被更新。

Essentially You/find Read the Object/Poco entity first. Change only those properties you want. And save. Only the changed properties are updated.

如果您不使用autoDetectChnages

If you are not using autoDetectChnages

 this.Configuration.AutoDetectChangesEnabled = false; ////<<<<<<<<< Default true



然后,你会打电话检测保存之前的变化。

Then you would Call Detect Changes before Saving.

但无论哪种方式的概念是基于一个读第一个拿到实体。
进行必要的修改和保存。

But either way the concept is based around a Read first to get entity. Make the necessary changes and save.

只有实际的变化被送回分贝。
例如:

Only the actually changes are sent back to Db. eg:

  var mypoco = Context.Set<TPoco>.Find(1);
  myPoco.propertyXyz = "changed";
  // normally not required by default, But incase your are not using tracking proxies , tell ef heads Up
  // Context.Context.ChangeTracker.DetectChanges(); // uncomment when needed
  Context.SaveChanged();



只有实际更改发送到数据库。

Only the actually changes are sent to DB.

虽然从Rameez的POST是正确的,它并没有说明为什么设置整个条目改变是可取的也不知道为什么这样做呢?为什么从文档链接国家入境后

Whilst the POST from Rameez is correct, it does not indicate why setting the whole entry as changed is desirable nor why do that ? Why link the State entry post from documentation ?

   Context.Entry(poco).State = state;  // why do this ? or the objectContext equivalent 

这将导致更新设置的所有值将数据库中的SaveChanges $ B为改变$ b由于所有字段将进行处理。这是不使用EF的好办法。

This will result in an UPdate Set for all values going to Database on SaveChanges Since ALL fields will be treated as changed. This is NOT a good way to use EF.

要了解汽车检测EF的变化是很重要的。
自动检测
和的实体状态和调用SaveChanges

It is important to know about the auto detect changes in EF. See Automatic detect changes and Entity states and SaveChanges

这篇关于当使用EF连接,是可以不覆盖我还没有更新的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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