Search⌘ K

Configuring One-to-Many Relationships

Explore how to configure one-to-many relationships between entities in Entity Framework Core. Understand default conventions, how to define navigation properties, and use data annotations and the fluent API to customize relationships. Gain practical knowledge on managing foreign keys, cascade deletes, and multiple navigations to build precise data models.

Overview

In this lesson, we review the conventions between two entities that automatically result in a one-to-many relationship between their corresponding tables in the database. Later we’ll see how to supplement these conventions using data annotations and the fluent API.

Note: The commands in this lesson generate code and files. Through the terminal, we can navigate to these files by using relevant Linux commands such as ls to view a list of files and directories, cd to change directories, and cat to view file contents. A SPA widget showing the updated project with the generated files is also available. Also, note that EF Core uses a timestamp within the generated file names. We represent these names with xxx.

Default conventions

There are several conventions by which we establish a one-to-many relationship between two entities. Let’s discuss a couple of them.

Single navigation property

We can create a one-to-many relationship between two entities by including a navigation property in one of the entities. We see this arrangement in the project below:

{
    "version": "0.2.0",
    "configurations": [
        {
            // Use IntelliSense to find out which attributes exist for C# debugging
            // Use hover for the description of the existing attributes
            // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
            "name": ".NET Core Launch (console)",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            // If you have changed target frameworks, make sure to update the program path.
            "program": "${workspaceFolder}/bin/Debug/net6.0/Relationships.dll",
            "args": [],
            "cwd": "${workspaceFolder}",
            // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
            "console": "internalConsole",
            "stopAtEntry": false
        },
        {
            "name": ".NET Core Attach",
            "type": "coreclr",
            "request": "attach"
        }
    ]
}
Configuring a one-to-many relationship

Click the “Run” button above, then execute the command below in the terminal:

C#
dotnet ef migrations add SingleNavigationPropertyWithCollectionProperty

After executing the code above, our updated project is below:

{
    "version": "0.2.0",
    "configurations": [
        {
            // Use IntelliSense to find out which attributes exist for C# debugging
            // Use hover for the description of the existing attributes
            // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
            "name": ".NET Core Launch (console)",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            // If you have changed target frameworks, make sure to update the program path.
            "program": "${workspaceFolder}/bin/Debug/net6.0/Relationships.dll",
            "args": [],
            "cwd": "${workspaceFolder}",
            // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
            "console": "internalConsole",
            "stopAtEntry": false
        },
        {
            "name": ".NET Core Attach",
            "type": "coreclr",
            "request": "attach"
        }
    ]
}
Project illustrating single navigation property with migrations

Note the following in the updated project:

  • There is a relationship between the Employee and Album entities where Employee is the principal entity while Album is the dependent entity.

  • An employee (artist) can have many albums, and we achieve this by including a ...