3.0 KiB
no-loc | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
::: moniker range=">= aspnetcore-3.0"
Create a Data folder.
Add the following MvcMovieContext
class to the Data folder:
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:
Add NuGet packages and EF tools
Register the database context
Add the following using
statements at the top of Startup.cs:
using MvcMovie.Data;
using Microsoft.EntityFrameworkCore;
Register the database context with the dependency injection container in Startup.ConfigureServices
.
Build the project as a check for compiler errors.
::: moniker-end
::: moniker range="< aspnetcore-3.0"
Add the following MvcMovieContext
class to the Models folder:
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:
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
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 MvcMovie.Models;
using Microsoft.EntityFrameworkCore;
Register the database context with the dependency injection container in Startup.ConfigureServices
.
Build the project as a check for errors. ::: moniker-end