1

Hey guys I am curious in how I can update just one column at a time in a database. When I run something such as postman or fiddle to query a Put to my database. If I only include one field, it sets all of the other fields = to null. Is there anyway I would be able to leave the other fields blank when I query the PUT and it will only change the one field I am asking the PUT to update? Sorry if my explanation is not good I am new to using API's.

Here is my PUT method (basic scaffold):

public IHttpActionResult PutUser(int id, User user)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }


            if (id != user.Id)
            {
                return BadRequest();
            }

            db.Entry(user).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!UserExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

When I want to update, say just the first name, and there is already a first and last name in the database. After the PUT method the last name would be set to null. So I debugged and noticed that the user it is taking in in the parameter already has last name set to null. Any advice would be greatly appreciated!

Shawn
  • 2,355
  • 14
  • 48
  • 98

1 Answers1

1

I'm guessing that you are sending a blank user with just information in the column that you want to update. Well what you should do is read the user details first, then change the column/field as required, then send this new user object back to the API Put method

Edit Basically you can't really specify a single column/field to update using this API pattern. Look at your constructor, there's no parameter to tell the code which column you want to update. (You could test for nulls, but I would strongly advise against it). Your code is telling EntityFramework to update the database with the User object that you provide. Therefore you need to first retrieve the user that you want to update (using a GET), and only change the field that you want to change, then send the updated User object back to the API Put method. If you send a User object with NULL values, then these NULL values will be saved to the database.

If you really want to be able to specify columns, you can write separate PUT method overloads that will only update the column you want. However programming this way requires more effort and more maintenance, and I would advise against it.

failedprogramming
  • 2,532
  • 18
  • 21
  • So, where it is PutUser(int id, User user). That user has already set everything to null if it is not included as a field I want to update. So if I did try to pull in the user's details for the user everything was already changed to the new PUT request? I am a little confused. – Shawn Mar 11 '14 at 22:44
  • 1
    I've added a bit more explanation in my answer – failedprogramming Mar 11 '14 at 23:02