...

/

Reviewing and Modifying the Scaffolded Model

Reviewing and Modifying the Scaffolded Model

Learn how to review and modify the scaffolded data model.

Overview

Previously, we scaffolded the following directories and files:

  • Data/ArtistsContext.cs
  • Entities/Album.cs
  • Entities/Employee.cs
  • Entities/Studio.cs
  • Entities/Tag.cs

In this lesson, we’ll:

  • Review these generated files.
  • Update the EF Core model after modifying the database.

Our 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/ScaffoldDB.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 with the generated model

ArtistsContext

  • On Line 6, the ArtistsContext class extends the DbContext class.

  • Lines 8–10 show the parameterless constructor of the ArtistsContext class. Calling this constructor creates a new instance of the DbContext class.

  • Lines 12–15 provide a constructor that allows creation of the DbContext class from options specified in the DbContextOptions object. Below is an example:

Press + to interact
var contextOptions = new DbContextOptionsBuilder<ArtistsContext>()
.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Sample")
.Options;

Then, we configure a DbContext with the following code:

Press + to interact
using var artistsContext = new ArtistsContext(contextOptions);
  • Lines 17–20 list the DbSet<TEntity> properties that have been ...