EntityFramework — Partial update rows in a table?

Good day.

There is a simple table in the database
CREATE TABLE [dbo].[Users]
(
 [Id] INT NOT NULL PRIMARY KEY, 
 [FirstName] VARCHAR(50) NOT NULL, 
 [LastName] VARCHAR(50) NOT NULL, 
 [Age] INT NOT NULL
)


How to write the controller method to update one(or more columns) ?
[HttpPatch]
 public ActionResult Patch([FromBody]User userData)
{
 var user = Db.Users.FirstOrDefault(entity => entity.Id== userData.Id);
 if (userTokenData == null)
{
 return null;
}

 user.FirstName = userData.FirstName; // ??
Db.SaveChanges();
 return Json(user);
 }


To be able to send requests like:
{ Id: 1, Age: 42 }
Updated should only column Age

{ Id: 1, FirstName: "Alex", LastName: "Ander" }
Updated to have columns FirstName and LastName
June 7th 19 at 14:33
1 answer
June 7th 19 at 14:35
Solution
_context.Entry(userData).State = EntityState.Modified;
try
{
 await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
 if (!Db.Users.Any( a => a.Id==userData.Id))
{
 return NotFound();
}
else
{
throw;
}
}

Find more questions by tags Entity FrameworkASP.NET