简体   繁体   中英

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

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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