expressappframework-402984-getting-started-in-depth-tutorial-blazor-define-data-model-and-set-initial-data-define-data-model-and-set-initial-data-with-ef-core-set-a-one-to-many-relationship-ef-core.md
This lesson explains how to create a One-to-Many relationship between two entities and how XAF generates the UI for such a relationship.
Note
Before you proceed, take a moment to review the previous lessons:
In the MySolution.Module\Business Objects folder, create the Department class. Replace the generated class declaration with the following code:
Go to the MySolution.Module\MySolutionDbContext file and add a DbSet of the Department class:
Add the Department reference property to the Employee class:
Add the Employees collection property to the Department class and initialize it in the constructor:
Run the application.
Open the Employee Detail View. In this view, XAF creates a lookup editor for the Department reference property. Lookup editors support incremental filtering. This editor uses a special type of View — Lookup List View. The Lookup List View includes a single column that displays the values of the default property. In your application, these are the values of the Title property.
Note
The most common pattern for a relationship is to define properties on both ends of the relationship. At the same time, according to the conventions of Entity Framework Core, it is sufficient to add only the reference property (the “One” part). It establishes the “One-To-Many” relationship between entities and Entity Framework Core automatically creates a foreign key to the related table in the database.
The main difference between these techniques is how XAF renders the application’s UI. When you omit the “Many” part of the relationship, XAF doesn’t create an editor for the omitted collection property in the Detail View of the entity class. You can see an example of this in the following lesson: Implement Reference Properties.
Create a PhoneNumber class and implement a One-To-Many relationship between this class and the Employee class. This time the Employee should be the “One” part of the relationship, while the PhoneNumber should be the “Many” part. You can find the type declaration in the code sample below.
Run the application. Open the Employee Detail View to see the Phone Numbers group:
Configure a Many-to-Many Relationship
See Also