0

I am using Entity Framework API and I am trying to update just one column using the Put method...

[ResponseType(typeof(void))]
        [Authorize]
        public IHttpActionResult PutLCTimeSlots(int id, LCTimeSlots lCTimeSlots)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

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

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


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

            return StatusCode(HttpStatusCode.NoContent);
        }

My question, what do I need to change in this method to only update one column?

I have tried replacing db.Entry(lCTimeSlots).State = EntityState.Modified; with db.Entry(lCTimeSlots).Property(x => x.taken).IsModified = true; But that didn't work....anyone have any ideas?

user979331
  • 11,039
  • 73
  • 223
  • 418

2 Answers2

0

You shouldn't use the default PUT for such an operation as it implies a client should be able to update more than a single property. I'd suggest a PUT operation using a route that describes the property being updated w/ parameter of the property's type instead of an LCTimeSlots object:

[HttpPut( "{id}/yourProperty" )]
[Authorize]
public IHttpActionResult YourProperty( int id, TypeOfProperty yourProperty )
{
    // validate value of `yourProperty` if you can before loading entity from DB

    // load entity from DB
    var loadedEntity = dbContext.Set<LCTimeSlots>().Find( id );

    // if not found, 404 it

    // update property, e.g.: 
    loadedEntity.YourProperty = yourProperty;

    // validate entity in its entirety if necessary

    // save changes
}
Moho
  • 15,457
  • 1
  • 30
  • 31
0

I'll start by suggesting use of the PATCH verb if you only want to modify certain properties.

Also, it's not a great idea to accept an entity object from the client, instead accept a model object that only has the properties that you aim to modify with this method.

Lastly, verify that the entity exists before attempting to make any change.

Now, do something like this:

        var timeSlot = db.LCTimeSlots.SingleOrDefault(e => e.Id == model.Id);

        if (timeSlot != null)
        {
            db.Entry(timeSlot).CurrentValues.SetValues(model);
            db.SaveChanges();
        }
        else
        {
            //404
        }
Byron Jones
  • 702
  • 5
  • 11
  • So do I want to replace this line `db.Entry(lCTimeSlots).State = EntityState.Modified;` with your code? – user979331 Sep 19 '18 at 16:36
  • @user979331 Yes -- you will be able to eliminate the try/catch as well. But again I strongly suggest not directly accepting an entity object from a user (this gives a user the ability to modify any property on the object, which, depending on what you're doing, may even be a security risk. Create a class having only the properties you want the user to be able to modify, and use that as the model. – Byron Jones Sep 19 '18 at 17:11