© Eric Vogel 2021
E. VogelBeginning Entity Framework Core 5https://doi.org/10.1007/978-1-4842-6882-7_9

9. Updating Data

Eric Vogel1  
(1)
Okemos, MI, USA
 

In this chapter, I will cover how to update existing data using Entity Framework Core 5. I will cover how to update the root entity and its associated child entities as well.

Updating the Root Entity

A simple way to update an entity is to first retrieve it and then modify the needed properties and commit the change through SaveChanges. For example, to update a record by Id, you would first retrieve the record by its Id, then change any property values, and commit the change through the context as follows:
using (var context = new AppDbContext())
{
    var person = context.Persons.Single(x => x.Id == 1); ...

Get Beginning Entity Framework Core 5: From Novice to Professional now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.