简体   繁体   English

仅使用实体框架和RIA服务更新已修改的列

[英]Update modified columns only, with Entity Framework and RIA services

I'm using Entity Framework, fronted by a client which is connected to the database using RIA Services. 我使用的是Entity Framework,该客户端带有使用RIA Services连接到数据库的客户端。 Whenever something is saved from the client, all the columns in the affected row is updated. 每当从客户端保存一些内容时,受影响行中的所有列都会更新。

Is this intentional behavior, or can I somehow force the changesets which propagate through the RIA domain services to update only the actually changed columns? 这是故意行为,还是我可以某种方式强制通过RIA域服务传播的变更集仅更新实际更改的列?

In your Update<T> method, you will have to do following... 在您的Update<T>方法中,您将必须执行以下操作...

You will have to do this for every Update method and you will need to get dbOriginal based on primary key for that particular object. 您将必须对每个Update方法执行此操作,并且需要基于该特定对象的主键获取dbOriginal。

// change state of entity as Unmodified/Unchanged...
original.EntityState = Unchanged;

// this is copy form database...
// Use different context
MyOrderContext context = new MyOrderContext();
Order dbOriginal = context.Orders.First( x=>x.OrderID == original.OrderID);

foreach(PropertyInfo p in copy.GetTypes().GetProperties()){
   Object originalValue = p.GetValue(dbOriginal);
   Object newValue = p.GetValue(original);
   if(originalValue!=null && newValue!=null 
       && originalValue.Equals(newValue)){
       continue;
   }
   // resetting this will 
   // make entity's only current
   // property as changed
   p.SetValue(original,originalValue);
   p.SetValue(original,newValue);
}

The unit of transfer/change in RIA Services (and Entity Framework for that matter) is an Entity. RIA服务(以及与此相关的实体框架)中的转移/更改单位是实体。

Why do you want want to only update columns that change when most of the time whole rows are written in business apps with SQL anyway? 您为什么只想在大多数情况下都使用SQL在业务应用程序中写入整行时更新那些会更改的列? It generally requires more processing power/code/time to only update specific columns of a row. 通常只需要更新一行的特定列就需要更多的处理能力/代码/时间。

If you want something that is more granular you will need to make entities per property of each row, but I woudl need a very string reason to do that. 如果您想要更细粒度的内容,则需要为每行的每个属性创建实体,但是我需要一个非常字符串化的理由来做到这一点。 Can you clarify your aims? 你能阐明你的目标吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM