Understanding one-to-many relationships
One-to-many relationships are the most common relationships in a relational database. They are also called parent-child (children) relationships. For example, an invoice has a collection of invoice items. In this section, we will learn how to configure a one-to-many relationship in EF Core and how to implement CRUD operations for entities with a one-to-many relationship.
Let us continue to use the invoice sample application. You can find the sample code of the EfCoreRelationshipsDemo
project in the chapter6
folder. If you would like to test the code following the book, you can continue to work on the BasicEfCoreDemo
project. Note that the InvoiceDbContext
class has been renamed SampleDbContext
in the sample code.
Next, let us update the Invoice
class and create an InvoiceItem
class, then define the one-to-many relationship between them.
One-to-many configuration
To demonstrate a one-to-many relationship, we need to add a new class...