Configuring Entity Types

Learn how to configure entity types using data annotations or the fluent API.

Overview

An entity type is a type included in the EF Core model. EF Core creates tables from entity types. In this lesson, we’ll review how to configure entity types using data annotations or 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

We’ll demonstrate the default conventions with the C# console app 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/ModelConfigurations.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 entity types using default conventions

Click the “Run” button above. Then we add migrations to see how the configurations are applied by executing the command below in the terminal:

Press + to interact
dotnet ef migrations add DefaultConventions

Next, we’ll update the database with the command below:

Press + to interact
dotnet ef database update

After the database update, 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/ModelConfigurations.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"
        }
    ]
}
Updated project showing default configuration of entity types

Note the following in the updated project:

  • We include the types in the model on lines 12–14 of ArtistsContext.cs. ...