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


}
Community
  • 1
  • 1
Brian Var
  • 6,029
  • 25
  • 114
  • 212
  • 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?) – David Sep 23 '16 at 14:29
  • is this solution working for you or what ? http://stackoverflow.com/a/39663331/1077309 – Sampath Sep 24 '16 at 16:26

1 Answers1

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

}
Sampath
  • 63,341
  • 64
  • 307
  • 441
  • 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. – David Sep 23 '16 at 14:32
  • this is the modify part `recordOriginal = recordModified;`.which is coming from his method.@David – Sampath Sep 23 '16 at 14:34
  • 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. – David Sep 23 '16 at 14:35
  • 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 – Sampath Sep 23 '16 at 14:41
  • 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. – David Sep 23 '16 at 14:43
  • yes,your'e right.I have updated the answer.please see that @David – Sampath Sep 23 '16 at 14:48