一:创建项目 添加 csproj (或者直接NuGet 引用)
<ItemGroup> <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.0.1"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> </PackageReference> <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="3.0.0" /> <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.3" /> </ItemGroup>
二:还原包,
CLI 命令 dotnet restore
三:DBFirst 创建脚手架,映射
CLI 命令:dotnet ef dbcontext scaffold "Server=127.0.0.1;port=3306;user=xxx;password=xxx;database=db" Pomelo.EntityFrameworkCore.MySql [-o 输出到文件夹目录] [-t 输出表的名字] [-t 输出表的名字]..
【dotnet ef dbcontext scaffold "Server=127.0.0.1;port=3306;user=xxx;password=xxx;database=db" Pomelo.EntityFrameworkCore.MySql -o models -t table1 -t table2.. 】
然后直接通过上下文对象就可以直接访问啦
using (var db = new dbContext()) { db.Table1.Take(100).ToList(); }
:修改 dbContext 中的连接字符串到配置文件