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.
© Eric Vogel 2021E. VogelBeginning Entity Framework Core 5https://doi.org/10.1007/978-1-4842-6882-7_9
9. Updating Data
Okemos, MI, USA
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); ...