2

I've modified an object locally and then pass it to the DAL to be updated on the connected database.

Usually I would use a stored procedure and execute reader to update the DB but this project implements a db context instead.

But when I run the method to save the changes it returns without error and the record isn't updated on the database.

Doing a search on here I came across this question suggesting to mark the db record as modified state before calling save. Which didn't correct the issue.

Question:

How can you push modified record to DB using dbcontext SaveChanges?

This is the gist of the DAL method:

public void update_Release_Status(Status recordModified)
{

          //Get the original record and update with the modified values.

        Status recordOriginal = db3.Status .First(i => i.ID == recordModified.ID);
        db3.Entry(recordOriginal).State = System.Data.Entity.EntityState.Modified; //marked as modified here before saving
        recordOriginal = recordModified;
        db3.SaveChanges();


}
2
  • You never actually changed anything on the instance of recordOriginal, so there's nothing to update. Can you set individual fields on recordOriginal instead of replacing the variable reference entirely? Or perhaps you can attach recordModified to the context if it originally came from a DB context? (Where did it come from?) Commented Sep 23, 2016 at 14:29
  • is this solution working for you or what ? stackoverflow.com/a/39663331/1077309 Commented Sep 24, 2016 at 16:26

1 Answer 1

4

Your entity is connected (or tracked) one.So you don't need to do like this db3.Entry(recordOriginal).State = System.Data.Entity.EntityState.Modified;

Note : You have to map your incoming object's properties to the fetched object.You can do that either using Mapper API or manually as shown below.

public void update_Release_Status(Status recordModified)
{

    Status recordOriginal = db3.Status.First(i => i.ID == recordModified.ID);

    recordOriginal.Name = recordModified.Name;//here you have to do the mapping
    recordOriginal.Age=recordModified.Age; //just used fake property names :)

    db3.SaveChanges();

}
Sign up to request clarification or add additional context in comments.

6 Comments

Wouldn't this have the exact same problem? The actual object fetched from the DB context is never modified. There are no changes to save.
this is the modify part recordOriginal = recordModified;.which is coming from his method.@David
Yes, but is recordModified attached to the DB context at all? The problem being experienced would imply that it isn't. And if it is, why fetch a separate instance anyway? Essentially you're fetching an object from the DB context and then never doing anything with that object. So why fetch it at all? The entire method you show here can be simplified to just the last line only.
No need to be attached this recordModified to the context. B'cos we're updating the attached or tracked recordOriginal entity.It's like this,let's say recordOriginal.Name="David"; and then db3.SaveChanges();.That is it. @David
That's not how reference variables work in C# (or in any language I've seen). recordOriginal = recordModified; doesn't individually update the properties on recordOriginal to match the properties on recordModified, it simply updates the variable reference itself to point to the other object. So the actual object which was previously referenced by the recordOriginal variable is still on the heap, unmodified, and immediately available for garbage collection because there are no references to it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.