0

I am using ASP.NET MVC Entity Framework and I created an API controller, now I want to add a method that is basically a copy of the put method, however I want to adjust this method so it updates a single column

[ResponseType(typeof(void))]
        [Authorize]
        public IHttpActionResult PutUser(int id, Users 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 (!UsersExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

each user in my User class has an email and column called isOnline, for this method I want to update the isOnline to true based on the email.

The examples I have seen online are for non API controllers. Please Help!

3
  • 2
    The code for EF is the same whether it is an api controller or an MVC controller. Commented Aug 22, 2017 at 21:17
  • What is your question exactly? Commented Aug 22, 2017 at 21:19
  • How do I adjust this method to update one column. Commented Aug 22, 2017 at 22:27

1 Answer 1

1

Your subject is partial update in Entity Framework, following is an example:

var user= new User() {Id = id, TargetColumn = "test"};
context.Users.Attach(user);
var entry = context.Entry(user);
entry.Property(e => e.TargetColumn ).IsModified = true;;
Sign up to request clarification or add additional context in comments.

Comments

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.