Scaffolding a database produces an Entity Framework model from an existing database. The resulting entities are created and mapped to the tables in the specified database. For an overview of the requirements to use EF Core with MySQL, see Table 4.3, “Connector/NET Versions and Entity Framework Core Support”).
NuGet packages have the ability to select the best target for a project, which means that NuGet installs the libraries related to that specific framework version.
There are two different ways to scaffold an existing database:
This section shows how to scaffold the sakila
database using both approaches. Additional scaffolding techniques
are:
For the components needed to reproduce each scaffolding approach, see General Requirements for EF Core Support. With the Package Manager Console approach, determine which version of Visual Studio is recommended for the version of .NET or .NET Core in use (see Table 4.1, “Connector/NET Requirements for Related Products”).
To download sakila
database, see
https://dev.mysql.com/doc/sakila/en/.
When upgrading ASP.NET Core applications to a newer framework, be sure to use the appropriate EF Core version (see https://docs.microsoft.com/en-us/aspnet/core/migration/30-to-31?view=aspnetcore-3.1).
Initialize a valid .NET Core project and console application using the .NET Core command-line interface (CLI) and then change to the newly created folder (
sakilaConsole
).dotnet new console –o sakilaConsole
cd sakilaConsole
Add the MySQL NuGet package for EF Core using the CLI. For example, use the following command to add the MySQL EF Core 7.0 package for use with Connector/NET 8.0.33 and later.
dotnet add package MySql.EntityFrameworkCore --version 7.0.2
Add the following
Microsoft.EntityFrameworkCore.Design
Nuget package:dotnet add package Microsoft.EntityFrameworkCore.Tools
Restore dependencies and project-specific tools that are specified in the project file as follows:
dotnet restore
Create the Entity Framework Core model by executing the following command. The connection string for this example must include
database=sakila
. For information about using connection strings, see Section 4.4.1, “Creating a Connector/NET Connection String”.NoteIf you are using a connector version earlier than Connector/NET 8.0.23, replace
MySql.EntityFrameworkCore
withMySql.Data.EntityFrameworkCore
.dotnet ef dbcontext scaffold "connection-string" MySql.EntityFrameworkCore -o sakila -f
To validate that the model has been created, open the new
sakila
folder. You should see files corresponding to all tables mapped to entities. In addition, look for thesakilaContext.cs
file, which contains theDbContext
for this database.
Open Visual Studio and create a new Console App (.NET Core) for C#.
Add the MySQL NuGet package for EF Core using the
. For example, use the following command to add the MySQL EF Core 7.0 package for use the Connector/NET 8.0.33 and later.Install-Package MySql.EntityFrameworkCore -Version 7.0.2
Install the following NuGet package by selecting either
(or and then ) from the menu:Microsoft.EntityFrameworkCore.Tools
.Open
and enter the following command at the prompt to create the entities andDbContext
for thesakila
database. The connection string for this example must includedatabase=sakila
. For information about using connection strings, see Section 4.4.1, “Creating a Connector/NET Connection String”.NoteIf you are using a connector version earlier than Connector/NET 8.0.23, replace
MySql.EntityFrameworkCore
withMySql.Data.EntityFrameworkCore
.Scaffold-DbContext "connection-string" MySql.EntityFrameworkCore -OutputDir sakila -f
Visual Studio creates a new
sakila
folder inside the project, which contains all the tables mapped to entities and thesakilaContext.cs
file.
It is possible to specify the exact tables in a schema to use when
scaffolding database and to omit the rest. The command-line
examples that follow show the parameters needed for filtering
tables. The connection string for this example must include
database=sakila
.
If you are using a connector version earlier than Connector/NET 8.0.23,
replace MySql.EntityFrameworkCore
with
MySql.Data.EntityFrameworkCore
.
.NET Core CLI:
dotnet ef dbcontext scaffold "connection-string" MySql.EntityFrameworkCore -o sakila -t actor -t film -t film_actor -t language -f
Package Manager Console in Visual Studio:
Scaffold-DbContext "connection-string" MySql.EntityFrameworkCore -OutputDir Sakila -Tables actor,film,language -f
When scaffolding a database, you can use more than one schema or database. Note that the account used to connect to the MySQL server must have access to each schema to be included within the context.
The following command-line examples show how to incorporate the
sakila
and world
schemas
within a single context. If you are using a connector version
earlier than Connector/NET 8.0.23, replace
MySql.EntityFrameworkCore
with
MySql.Data.EntityFrameworkCore
.
.NET Core CLI:
dotnet ef dbcontext scaffold "connection-string" MySql.EntityFrameworkCore -o sakila --schema sakila --schema world -f
Package Manager Console in Visual Studio:
Scaffold-DbContext "connection-string" MySql.EntityFrameworkCore -OutputDir Sakila -Schemas sakila,world -f