Alex Kibler
Alex Kibler

Reputation: 4954

Entity Framework 6 db.savechanges isn't saving changes to database

I've got an API where I can POST an object and update the matching record in the database. That endpoint looks like this:

// POST: api/Spell
public IHttpActionResult Post([FromBody]Spell spell)
{
    using (SpellContext db = new SpellContext())
    {
        var recordToUpdate = db.Spells.Where(x => x.id == spell.id).FirstOrDefault();

        if (recordToUpdate != null)
        {
            recordToUpdate = spell;
            db.SaveChanges();
            return Ok("Spell " + spell.id + " (" + spell.name + ") Updated Successfully!");
        }
        else
        {
            return InternalServerError();
        }
    }
}

So basically as long as a spell with the same ID as the incoming spell exists, we save changes.

When I call it, I'm returning Ok, but nothing is updating in the database.

How come?

Upvotes: 1

Views: 639

Answers (2)

Eric J.
Eric J.

Reputation: 150188

The instance of spell that you get from the database is detached from the DbContext.

public IHttpActionResult Post([FromBody]Spell spell)

You need to explicitly set the entity state to Modified

if (recordToUpdate != null)
{
    // Not needed: recordToUpdate = spell;
    db.Entry(spell).State = EntityState.Modified;
    db.SaveChanges();
    return Ok("Spell " + spell.id + " (" + spell.name + ") Updated Successfully!");
}

Note that you do not need to explicitly assign spell to recordToUpdate as long as they both have the same entity key.

Upvotes: 1

Alex Kibler
Alex Kibler

Reputation: 4954

My issue was that I needed to be setting each of the parameters individually instead of just setting one object to equal another.

Updated API Call:

public IHttpActionResult Post([FromBody]Spell spell)
        {
            using (SpellContext db = new SpellContext())
            {

                var recordToUpdate = db.Spells.Where(x => x.id == spell.id).FirstOrDefault();

                if (recordToUpdate != null)
                {
                    recordToUpdate.name = spell.name;
                    recordToUpdate.desc = spell.desc;
                    recordToUpdate.higher_level = spell.higher_level;
                    recordToUpdate.page = spell.page;
                    recordToUpdate.range = spell.range;
                    recordToUpdate.components = spell.components;
                    recordToUpdate.material = spell.material;
                    recordToUpdate.ritual = spell.ritual;
                    recordToUpdate.duration = spell.duration;
                    recordToUpdate.concentration = spell.concentration;
                    recordToUpdate.casting_time = spell.casting_time;
                    recordToUpdate.level = spell.level;
                    recordToUpdate.school = spell.school;
                    recordToUpdate.class_name = spell.class_name;
                    db.SaveChanges();
                    return Ok("Spell " + spell.id + " (" + spell.name + ") Updated Successfully!");
                }
                else
                {
                    return InternalServerError();
                }

            }
        }

Upvotes: 0

Related Questions