domain service - server side update
I have a silverlight 4 RIA Domain Service. In one particular method I need to change one value in the database (among other things of course). To do this I get the entity, change the one value, then I need to save that change back to the DB.
I've tried calling the entity's generated update function, which just calls this.ObjectContext.myEntity.AttachAsModified(myENtity); but the change never gets back to the database.
How do I save values from the sever side (ie. the cli开发者_如何转开发ent never had this data)?
You should know that the UpdateXXX method doesn't actually submit the changes to the database - that only happens a bit later. knowing this, we can change the default implementation of the UpdateXXX method: (I'm assuming XXX == Product here)
public void UpdateProduct(Product currentProduct)
{
this.ObjectContext.Products.AttachAsModified(currentProduct, this.ChangeSet.GetOriginal(currentProduct));
}
to
public void UpdateProduct(Product currentProduct)
{
// This line only reattach the entity back to EF context, it doesn't submit changes yet
this.ObjectContext.Products.AttachAsModified(currentProduct, this.ChangeSet.GetOriginal(currentProduct));
currentProduct.SomeProperty = "SomeChange"; // This change is going to be submitted later
}
Turns out, attaching the object before or after the change makes no difference. The missing peice was :
this.ObjectContext.SaveChanges();
精彩评论