1

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?

1
  • So I realized that I needed to be setting the parameters all individually. Does anyone know how I can do this programatically so I don't have to type them all next time? Commented Oct 7, 2015 at 22:40

2 Answers 2

1

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.

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

Comments

0

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

            }
        }

1 Comment

That is the hard way, especially if you have a deep object graph. Did you try my solution?

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.