I have some confusion, stemming from this http://msdn.microsoft.com/en-us/library/vstudio/bb896248(v=vs.100).aspx regarding modifying a detached object.
That seems to in
This ended up being so easy. I don't know why I didn't understand it. Here is the solution to this:
private void UpdateWithOriginal(Category cat, string name)
{
Category updatedObject = new Category()
{
CategoryID = cat.CategoryID,
Name = cat.Name,
Topics = cat.Topics
};
updatedObject.Name = name;
using (TestContext ctx = new TestContext())
{
ctx.Categories.Attach(updatedObject);
ctx.ApplyOriginalValues("Categories", cat);
var state = ctx.ObjectStateManager.GetObjectStateEntry(updatedObject).State; //never modified state here
ctx.SaveChanges();
}
}
What I was missing is this. You have two objects, an original and an updated. You detach, send one off to a WCF service, changes are made, object is sent back, on your end you recreate the object as an updated object. Now in order to update your context, you need two objects, because with the given code, if you attach to your context the updatedObject, the status of your entity is this:
There is no different, so .SAveChanges() won't do anything. Since you attatched the updated object, you MUST use ApplyOriginalVAlues("Categories",OriginalCategory) to cause this:
Now you have a modified object and when you call .SaveChanges() the change will take effect. The reverse of this is true if you attach the original object (you will need to not modify the original value, but the current value, so you use ApplyCurrentVAlues()).