MySql Scaffolding an Existing Database in EF Core
官方文檔詳見:https://dev.mysql.com/doc/connector-net/en/connector-net-entityframework-core-scaffold-example.html
Scaffolding a Database Using Package Manager Console in Visual Studio
-
Open Visual Studio and create a new Console App (.NET Core) for C#.
-
Add the MySQL NuGet package for EF Core using the
NoteInstall-Package MySql.Data.EntityFrameworkCore -Version 8.0.13
The version (for example,
-v 8.0.13
) must match the actual Connector/NET version you are using. For current version information, seeTable 9.2, “Supported versions of Entity Framework Core”. -
Install the following NuGet packages by selecting either
-
Microsoft.EntityFrameworkCore.Design
EF Core 1.1 only: Also add the
MySql.Data.EntityFrameworkCore.Design
package. -
NoteMicrosoft.EntityFrameworkCore.Tools version 1.1.6
(for EF Core 1.1) andMicrosoft.EntityFrameworkCore.Tools version 2.0.3
(for EF Core 2.0)The .NET tools are included in the .NET Core 2.1 SDK and not required or supported for EF Core 2.1. If this is an upgrade, remove the reference to that package from the .
csproj
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.3" />
-
-
Open
Scaffold-DbContext "server=localhost;port=3306;user=root;password=mypass;database=sakila" MySql.Data.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.
MySql Scaffolding an Existing Database in EF Core