Entity Framework Modify Detached Object

前端 未结 2 1830
忘了有多久
忘了有多久 2021-01-25 22:40

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

相关标签:
2条回答
  • 2021-01-25 23:19

    I think you're just misreading the MSDN article..

    From the MSDN Definition of ApplyOriginalValues:

    Copies the scalar values from the supplied object into set of original values for the object in the ObjectContext that has the same key.

    From the MSDN Definition of ApplyCurrentValues:

    Copies the scalar values from the supplied object into the object in the ObjectContext that has the same key.

    Which is exactly the behavior you're seeing. ApplyOriginalValues goes back to the database and updates the values. ApplyCurrentValues uses the values in the object that you have, so you can update the changes.

    0 讨论(0)
  • 2021-01-25 23:21

    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:

    • Original Values- Name = Bob
    • Current VAlues- Name = Bob

    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:

    • Original VAlue: Name = Steve
    • Current Value: Name = Bob

    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()).

    0 讨论(0)
提交回复
热议问题