...
/Conventions in Entity Framework Core
Conventions in Entity Framework Core
Learn about EF Core’s default conventions.
Overview
EF Core follows a set of rules when building a model. Those rules are collectively known as conventions. In this lesson, we review the default conventions that EF Core follows.
From now on, we’ll utilize the Code First workflow in our demonstrations. Recall that the Code First workflow makes use of migrations. Migrations represent a snapshot of the model at a point in time. Reviewing the migrations enables us to see what configurations are specified.
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
To illustrate the default conventions, we’ll work 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" } ] }
Add a migration by clicking the “Run” button on the SPA widget above, and then execute the command below in the terminal:
dotnet ef migrations add DefaultConventions
Next, let’s update the database with the following command:
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" } ] }
Note the following changes in the updated project, captioned “Updated project illustrating default conventions.”
The project has a context class named ArtistsContext
in ...