mysql .net core 3.0 entityframework
It works
I use nuget to download
7.2.2 Scaffolding an Existing Database in EF Core
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. This feature was introduced in MySQL Connector/NET 6.10.2-beta and 8.0.8-dmr; however, scaffolding is not supported with all versions of Connector/NET (see Table 7.2, “Supported versions of Entity Framework Core”).
The Design package for scaffolding a database is part of the main package in EF Core 2.0 (or later) and no longer separate. If you are upgrading from EF Core 1.1 to EF Core 2.0 or 2.1, you must remove the MySql.Data.EntityFrameworkCore.Design package manually.
NuGet packages have the ability to select the best target for a project, which means that NuGet will install 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.
Any implementation of .NET Standard (see https://dotnet.microsoft.com/platform/dotnet-standard#versions)
Visual Studio 2017 version 15.7 (required for Using Package Manager Console in Visual Studio)
MySQL Server 5.7
sakiladatabase sample (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 sakilaConsolecd sakilaConsoleAdd the MySQL NuGet package for EF Core using the CLI. For example, use the following command to add the
MySql.Data.EntityFrameworkCore v8.0.20package:dotnet add package MySql.Data.EntityFrameworkCore --version 8.0.20NoteThe version (for example,
--version 8.0.20) must match the actual Connector/NET version you are using. For current version information, see Table 7.2, “Supported versions of Entity Framework Core”.Add the following
Microsoft.EntityFrameworkCore.DesignNuget package:dotnet add package Microsoft.EntityFrameworkCore.DesignIf you are using EF Core 2.0, add a reference to
Microsoft.EntityFrameworkCore.Tools.DotNetas aDotNetCliToolReferenceentry in thesakilaConsole.csprojfile as follows:<ItemGroup> <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.3"/> </ItemGroup>NoteThe .NET tools are included in the .NET Core 2.1 SDK (and later) and are not required or supported for EF Core 2.1. If this is an upgrade, remove the reference to that package from the .
csprojfile (version 2.0.3 in this example) :<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.3" />Restore dependencies and project-specific tools that are specified in the project file as follows:
dotnet restoreCreate the Entity Framework Core model by executing the following command (adjust the connection-string values to match your settings for the
user=andpassword=options):dotnet ef dbcontext scaffold "server=localhost;port=3306;user=root;password=mypass;database=sakila" MySql.Data.EntityFrameworkCore -o sakila -fTo validate that the model has been created, open the new
sakilafolder. You should see files corresponding to all tables mapped to entities. In addition, look for thesakilaContext.csfile, which contains theDbContextfor 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.Data.EntityFrameworkCore v8.0.20package:Install-Package MySql.Data.EntityFrameworkCore -Version 8.0.20NoteThe version (for example,
-Version 8.0.20) must match the actual Connector/NET version you are using. For current version information, see Table 7.2, “Supported versions of Entity Framework Core”.Install the following NuGet packages by selecting either or from the and then menu:
Microsoft.EntityFrameworkCore.DesignMicrosoft.EntityFrameworkCore.Tools version 2.0.3(for EF Core 2.0)NoteThe .NET tools are included in the .NET Core 2.1 SDK (and later) and are not required or supported for EF Core 2.1. If this is an upgrade, remove the reference to that package from the .
csprojfile (version 2.0.3 in this example) :<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.3" />
Open and enter the following command at the prompt to create the entities and
DbContextfor thesakiladatabase (adjust the connection-string values to match your settings for theuser=andpassword=options):Scaffold-DbContext "server=localhost;port=3306;user=root;password=mypass;database=sakila" MySql.Data.EntityFrameworkCore -OutputDir sakila -fVisual Studio creates a new
sakilafolder inside the project, which contains all the tables mapped to entities and thesakilaContext.csfile.
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.
.NET Core CLI:
dotnet ef dbcontext scaffold "server=localhost;port=3306;user=root;password=mypass;database=sakila" MySql.Data.EntityFrameworkCore -o sakila -t actor -t film -t film_actor -t language -fPackage Manager Console in Visual Studio:
Scaffold-DbContext "server=localhost;port=3306;user=root;password=mypass;database=sakila" MySql.Data.EntityFrameworkCore -OutputDir Sakila -Tables actor,film,film_actor,language -fWhen 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. Multiple-schema functionality was introduced in Connector/NET 6.10.3-rc and 8.0.9-dmr releases.
The following command-line examples show how to incorporate the sakila and world schemas within a single context.
.NET Core CLI:
dotnet ef dbcontext scaffold "server=localhost;port=3306;user=root;password=mypass;database=sakila" MySql.Data.EntityFrameworkCore -o sakila --schema sakila --schema world -fPackage Manager Console in Visual Studio: <- I use this command
Scaffold-DbContext "server=localhost;port=3306;user=root;password=mypass;database=sakila" MySql.Data.EntityFrameworkCore -OutputDir Sakila -Schemas sakila,world -fReference
https://dev.mysql.com/doc/connector-net/en/connector-net-entityframework-core-scaffold-example.html
留言
張貼留言