Configuring Entity Properties
Learn how to configure entity properties using data annotations or fluent API.
We'll cover the following...
Overview
Each entity type in the model has a set of properties. In this lesson, we’ll review how to configure entity properties using data annotations or the fluent API. In other words, we’ll learn how to configure the property to column mappings.
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, andcat
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 withxxx
.
Default conventions
To demonstrate, 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" } ] }
Notice that the entity types have public properties. Click the “Run” button of the project. Then, we’ll add migrations to see how the configurations are applied by executing the command below in the terminal:
dotnet ef migrations add DefaultConventions
Next we’ll 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 showing default configuration of entity properties”:
- Lines 16–20, 31–34, and 45–48