在2020年,随着.NET Core 3的发布,很多事情都发生了变化,下面是当前适用的情况:)
确保您安装了所有这些包
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="3.1.1" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql.Design" Version="1.1.2" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.1" />
现在dotnet ef是一个需要安装的全局工具,参考
dotnet tool install --global dotnet-ef
然后运行这个命令,参考
dotnet ef dbcontext scaffold "Server=localhost
若要在不同路径中进行脚手架,请使用 --output-dir
dotnet ef dbcontext scaffold server=localhost;port=3306;database=palle2patnam;uid=root;password=password" "Pomelo.EntityFrameworkCore.MySql" --output-dir Models
如果需要从数据库中强制获取新字段,您可以添加--force参数。
dotnet ef dbcontext scaffold "server=localhost;port=3306;database=palle2patnam;uid=root;password=password" "Pomelo.EntityFrameworkCore.MySql" --output-dir Models --force
Scaffold-DbContext
方法将基于现有数据库创建 DbContext 和 Models,但是在脚手架之后,您需要继续使用“Code First”,因为没有方法可以使用 db 更改更新 DbContext。 - Valuator