© 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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.