AspNetCore.Docs/aspnetcore/includes/RP/model2.md

3.2 KiB

Add a database context class

In the RazorPagesMovie project, create a new folder called Data. Add the following RazorPagesMovieContext class to the Data folder:

[!code-csharp]

The preceding code creates a DbSet property for the entity set. In Entity Framework terminology, an entity set typically corresponds to a database table, and an entity corresponds to a row in the table.

Add a database connection string

Add a connection string to the appsettings.json file as shown in the following highlighted code:

::: moniker range=">= aspnetcore-3.0"

[!code-json]

Add NuGet packages and EF tools

Open a terminal for the RazorPagesMovie project. Right click the project name in the design/layout bar and go to Tools > Open in Terminal. Run the following .NET Core CLI commands in the Termial:

dotnet tool install --global dotnet-ef --version 3.0.0-*
dotnet add package Microsoft.EntityFrameworkCore.SQLite --version 3.0.0-*
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design --version 3.0.0-*
dotnet add package Microsoft.EntityFrameworkCore.Design --version 3.0.0-*
dotnet add package Microsoft.EntityFrameworkCore.SqlServer --version 3.0.0-*

The preceding commands add Entity Framework Core Tools for the .NET CLI and several packages to the project. The Microsoft.VisualStudio.Web.CodeGeneration.Design package is required for scaffolding.

Register the database context

Add the following using statements at the top of Startup.cs:

using RazorPagesMovie.Models;
using Microsoft.EntityFrameworkCore;

Register the database context with the dependency injection container in Startup.ConfigureServices.

[!code-csharp]

::: moniker-end

::: moniker range="< aspnetcore-3.0"

[!code-json]

Add required NuGet packages

Run the following .NET Core CLI command to add SQLite and CodeGeneration.Design to the project:

dotnet add package Microsoft.EntityFrameworkCore.SQLite
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design
dotnet add package Microsoft.EntityFrameworkCore.Design

The Microsoft.VisualStudio.Web.CodeGeneration.Design package is required for scaffolding.

Register the database context

Add the following using statements at the top of Startup.cs:

using RazorPagesMovie.Models;
using Microsoft.EntityFrameworkCore;

Register the database context with the dependency injection container in Startup.ConfigureServices.

[!code-csharp]

Build the project as a check for errors. ::: moniker-end