开发者

WCF RIA Services SP1, Entity Framework 4, updating only changed columns

I use LinqToEntitiesDomainService class to update database with Silverlight 4 client. There's AttachAsModified extended method for entity framework ObjectContext which allows you supply original entity property values:

Order original = this.ChangeSet.GetOriginal(currentOrder);
this.ObjectContext.Orders.AttachAsModified(currentOrder, original);

By default, WCF RIA Services doesn't send original values to the server, so one needs to apply [RoundtripOriginal()] attribute to his/her entity.

However, even if I supply original values, SQL generated by Entity framework updates all col开发者_StackOverflow社区umns, not only changed ones. Since AttachAsModified() method isn't native ObjectContext class method (it's extended method defined in ObjectContextExtensions class), I tried to use ApplyOriginalValues method which is defined in ObjectSet class. No change. It seems entity framework 4.1, which was released recently may have solution (not sure). How about entity framework 4? Is it possible EF to generate sql to update only changed columns?


AttachAsModified will mark the entity as modified. Subsequently (quote from MSDN):

When you change the EntityState of an entity object entry to Modified, all of the properties of the object are marked as modified, regardless of the current or original values.

Caveat; I haven't done this but, it should work.

Instead of using AttachAsModified, mark the entity as UnChanged using the ChangeState method.

Then use the SetModifiedProperty method on the properties that have changed to have them included in an update.

EDIT: If you want a way to find which properties have changed, there are a couple of articles out there explaining how to do so using the ObjectStateManager such as this one


I did ask similar question on MSDN forums, and it is confirmed that WCF RIA Services will change all columns. Alternative is,

You can fetch a copy from database, compare and mark SetModifiedProperty manually by using reflection.

// 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);
}

You may have to change code as per situation, check if property is readonly or not and this is just a sample but it will help you to build upon it.


I managed to do this by first attaching the object and then calling ApplyOriginalValues on the EntitySet. You'll need an object with the original values to do this. This method can also be used to prevent a column from being updated, e.g. for row level security.

NOTE: This unfortunately does not work without retrieving the original entity from the database first. Otherwise only properties that are set to its default value are excluded from the update...

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜