我正在尝试在ASP.NET Core Web应用程序中创建迁移,但是当我尝试执行迁移时,程序包管理器控制台将返回警告和错误:
警告:“还安装了版本低于6.3的Entity Framework。较新的工具正在运行。对于较旧的版本,请使用'EntityFramework \ Add-Migration'。”
错误:“在程序集'MyProjectName'中未找到任何迁移配置类型。(在Visual Studio中,您可以使用程序包管理器控制台中的Enable-Migrations命令来添加迁移配置)。”
经过研究后,我发现一个普遍的问题是在程序包管理器中选择的默认项目不正确。但这不是我的情况,因为它只给我一个选择,而这正是我正在从事的项目。我还发现当前的解决方案是使用“ Enable-Migrations”命令启用迁移,但是当我尝试执行此操作时,它给出了相同的警告和另一个错误:
错误:在程序集“ MyProjectName”中找不到上下文类型。
我还以另一种方式尝试了“ enable-migraitons”命令,即“ enable-migrations -contexttypename MyDBContextName”,但这给了我另一个错误:
错误:在程序集“ MyProjectName”中找不到上下文类型“ MyDBContextName”。
但是我实际上有以下课程:
public class MyDBContextName: IdentityDbContext<UserModel>
{
public MyDBContextName(DbContextOptions<MyDBContextName> options) : base(options)
{
}
}
我在启动/配置服务类中有这个:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<MyDBContextName>(options => options.UseSqlServer("DefaultConnection"));
services.AddIdentity<UserModel, IdentityRole>().AddEntityFrameworkStores<MyDBContextName>().AddDefaultTokenProviders();
services.AddControllers();
}
任何可能导致这些错误的想法?非常感谢您的宝贵时间,如果您需要更多信息,我们将在收到您的请求后尽快将其提供给您。祝你有美好的一天。(:
我仍然不知道是什么原因导致了这些错误,但是现在进行了一些更改之后,我就可以创建迁移了。这是我进行的3项更改:
1)我安装了Microsoft.EntityFrameworkCore.Tools
2)我换services.AddDbContext<MyDBContextName>(options => options.UseSqlServer("DefaultConnection"));
了services.AddDbContext<MyDBContextName>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
3)最后,在我的app.setting.json中,我进行了更改
"connectionStrings":
{
"DefaultConnection": "Data Source=JaimeLenovo;Initial Catalog=PracticaApiSecurity;Integrated Security=True"
},
对于
"connectionStrings": {
"DefaultConnections": "Data Source=JaimeLenovo;Initial Catalog=PracticaApiSecurity;Integrated Security=True"
},
更改连接的连接