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

8. Inserting Data

Eric Vogel1  
(1)
Okemos, MI, USA
 

Now that we learned how to retrieve data from our database using Entity Framework Core 5, let us look at how to insert new data into our database. I will cover how to insert into a table and its child tables by the use of its navigation properties.

Inserting the Root Entity

The code to insert a root into a database using Entity Framework Core 5 is simple. New records are added via the DbSet<T>.Add method. For example, to add a new person record without any addresses to our database using EF Core, you could use the following code:
using (var context = new AppDbContext())
{
    context.Persons.Add(new ...

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.