开发者

Update modified columns only, with Entity Framework and RIA services

开发者 https://www.devze.com 2023-04-04 19:08 出处:网络
I\'m using Entity Framework, fronted by a client which is connected to the database using RIA Services. Whenever something is saved from the client, all the columns in the affected row is updated.

I'm using Entity Framework, fronted by a client which is connected to the database using 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开发者_如何学JAVA domain services to update only the actually changed columns?


In your Update<T> method, you will have to do following...

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.

// 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.

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? 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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号