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 indicate if I modify an object in a detached state, when I reattach, I should use ApplyOriginalValues instead of ApplyCurrentValues. However, when I do this, it seems to never update the object as in the example, unless I modify the object after reattaching it. However if I modify after attaching, then it doesn't seem to matter which of these I use (applyoriginal or applycurrent).
Here is my code:
//this never works
private void UpdateWithOriginal(Category cat, string name)
{
using (TestContext ctx = new TestContext())
{
cat.Name = name;
ctx.Categories.Attach(cat);
ctx.ApplyOriginalValues("Categories", cat);
var state = ctx.ObjectStateManager.GetObjectStateEntry(cat).State; //never modified state here
ctx.SaveChanges();
}
}
//this always works
private void UpdateWithCurrent(Category cat, string name)
{
using (TestContext ctx = new TestContext())
{
ctx.Categories.Attach(cat);
cat.Name = name;
ctx.ApplyCurrentValues("Categories", cat);
var state = ctx.ObjectStateManager.GetObjectStateEntry(cat).State;
ctx.SaveChanges();
}
}
Does anyone know why the MSDN link seems to indicate that the //this never works, bit should work?