1

I am trying to update a field with various related tables using the EntityState.Modified but SaveChanges() is not applying the changes. I am using postman to post to the localhost/8000/api/co/1 and the status says 202 accepted. My repository is returning the record but it is not modified at all.

Question What am I missing in my repository for the changes to take in affect? Do I need to set each property individually? (I have hundreds)

Most the examples I can find related to the repository pattern and the entity framework 7, only utilize the EntityState.Modified(), then save changes. Can someone please help me point out what I am missing? I have my other repositories working fine that create, delete and get working just fine

My Repository

public COMP Update(int id)
{
    var original = _context.Complaints.Where(c => c.COMP_ID == id).FirstOrDefault<COMPLAINT>();
    _context.Entry(original).State = EntityState.Modified;
    SaveAll();
    return original;
}

public bool SaveAll()
{
    return _context.SaveChanges() > 0;
}
1
  • 1. you never actually assign anything to your object like a property change. 2. once retrieved that instance is tracked by default so changing the state should not be necessary. Commented Apr 6, 2016 at 16:37

1 Answer 1

1

I don't see where you are actually make any changes to entity at all. You should be pulling the original entity, make your changes and then call SaveChanges() to commit them :

public COMP Update(int id, string name)
{
    // Grab your entity
    var original = _context.Complaints.FirstOrDefault(c => c.COMP_ID == id);
    // Make your changes here (using a parameter)
    original.Name = name;
    // Save your changes
    SaveAll();
    // Return your original entity with the changes made
    return original;
}

In the above scenario, let's say you used Postman to update this particular entity and passed in the id and name as parameters (for example purpose, we will assume that this method just updates the name).

If you do that, then you could use the name parameter to update your existing entity as such :

// Make your changes here (using a parameter)
original.Name = name;

Then calling the SaveChanges() method will actually perform the update in your database.

If you have Change Tracking enabled on your context, you shouldn't need to worry about manually setting the modified states either.

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

12 Comments

Thank you for the swift response! Could you edit your answer to provide a sample of //make your changes here comment so I know which tutorial/sample to look up? I don't want to hardcode the changes but for it to accept a value from postman or from the webpage. Would it look something like if(original != null) { original.value= this.value}? If so, if I have hundreds of values, I would have to create one for each even the children values?
How you design it is going to be a matter of preference. If you were performing an update, you might consider "building" an object to represent your model (containing all of the relevant fields) and then when you call Update pass this model into it. Then within the method itself, you could use the id property of your Model to actually look up the entity and then update each of your properties accordingly (either directly setting them to the value from the model or checking if they changed). Does that make sense?
It does, I thought my method was already doing the entity method. For instance I passed the original entity in finding it by id then I used entity.modified to check the track changes, then I saved, then I returned the original to see the changes? I guess I am not understanding the make my changes part as I thought the modified part is doing that job? Can you please elaborate on "building" and object to represent your model? Does that mean, the sample that you provided?
You can see a very rough example here, which hopefully demonstrates what I am referring to.
Thanks for your answer,that worked perfectly fine. I am going to have to work with the model version to get it to work because I have multiple related tables. I've tried multiple times and keep getting the HttpNotFound output.
|

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.