Back to Devexpress

Configure a One-to-Many Relationship

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

latest3.8 KB
Original Source

Configure a One-to-Many Relationship

  • Jul 30, 2025
  • 5 minutes to read

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:

Employee-Department Relationship

  1. In the MySolution.Module\Business Objects folder, create the Department class. Replace the generated class declaration with the following code:

  2. Go to the MySolution.Module\MySolutionDbContext file and add a DbSet of the Department class:

  3. Add the Department reference property to the Employee class:

  4. Add the Employees collection property to the Department class and initialize it in the constructor:

  5. Run the application.

  6. 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.

Exercise: Create an Employee-PhoneNumber Relationship

  1. 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.

  2. Run the application. Open the Employee Detail View to see the Phone Numbers group:

Next Lesson

Configure a Many-to-Many Relationship

See Also

Data Annotation Attributes

Relationships Between Persistent Objects in Code and UI