1.问题描述
问题描述:
使用Blazor添加迁移时出现如下错误:
The specified deps.json [C:\Project\Practice\202004\BlazorWebApp\BlazorWebApp\bin\Debug\net5.0\BlazorWebApp.deps.json] does not exist
2.解决问题思路
1、试试用dotnet ef CLI 进行迁移操作(官方文档)。
(1)相关命令如下所示:
//全局安装工具
dotnet tool install --global dotnet-ef
//更新工具
dotnet tool update --global dotnet-ef
//项目中需安装
dotnet add package Microsoft.EntityFrameworkCore.Design
//添加新迁移
dotnet ef migrations add "迁移名"
//更新数据库
dotnet ef database update
(2)添加新迁移时,出现了如下错误,原因是在startup.cs中我未进行ef的相关配置。
(3)在startup.cs"中否配置EF,在appsetting.json中数据库连接字符串
相关配置如下:
services.AddDbContext<MyDbContext>(opt =>
opt.UseSqlServer(Configuration.GetConnectionString("MyDbContext"))
);
"ConnectionStrings": {
"MyDbContext": "Server=.;Database=CovidAPI;User ID=sa;Password=3344520;Trusted_Connection=False"
}
2、此时再次在"程序包管理器控制台"中添加迁移,依旧是出现如下错误:
3、尝试在cmd中使用dotnet ef添加迁移,此时使用可以dotnet ef migrations add initial
命令可迁移成功
C:\Project\Practice\202004\BlazorWebApp\WebApp.Server>dotnet ef migrations add initial
Build started...
Build succeeded.
Done. To undo this action, use 'ef migrations remove'
4、使用dotnet ef database update
命令可成功更新数据库
C:\Project\Practice\202004\BlazorWebApp\WebApp.Server>dotnet ef database update
Build started...
Build succeeded.
Done.
5、此时可看到数据库创建成功
注:使用程序包管理控制台失败的原因还未找到,后续找到了会在文章中更新