如何创建没有 "No migrations configuration type was found in the assembly" 问题的迁移?
How to create a migration without "No migrations configuration type was found in the assembly" problem?
我正在尝试在 ASP.NET 核心 Web 应用程序中创建迁移,但是当我尝试这样做时,程序包管理器控制台 returns 出现警告和错误:
警告:"A version of Entity Framework older than 6.3 is also installed. The newer tools are running. Use 'EntityFramework\Add-Migration' for the older version."
错误:"No migrations configuration type was found in the assembly 'MyProjectName'. (In Visual Studio you can use the Enable-Migrations command from Package Manager Console to add a migrations configuration)."
经过一番研究,我发现一个常见的问题是包管理器中选择的默认项目不正确;但这不是我的情况,因为它只给了我一个选择,而且这是我正在处理的项目。
我还发现当前的解决方案是使用 "Enable-Migrations" 命令启用迁移,但是当我尝试这样做时,它给了我同样的警告和另一个错误:
错误:在程序集中找不到上下文类型 'MyProjectName'。
我也以不同的方式尝试了 "enable-migraitons" 命令,"enable-migrations -contexttypename MyDBContextName",但这给了我另一个错误:
错误:在程序集 'MyProjectName' 中找不到上下文类型 'MyDBContextName'。
但我实际上有以下class:
public class MyDBContextName: IdentityDbContext<UserModel>
{
public MyDBContextName(DbContextOptions<MyDBContextName> options) : base(options)
{
}
}
我的 startup/configure 服务中有这个 class:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<MyDBContextName>(options => options.UseSqlServer("DefaultConnection"));
services.AddIdentity<UserModel, IdentityRole>().AddEntityFrameworkStores<MyDBContextName>().AddDefaultTokenProviders();
services.AddControllers();
}
最后, 是我的 NuGet 包的图片:
知道可能导致这些错误的原因吗?
非常感谢您抽出宝贵时间,如果您需要更多信息,我会在看到您的请求后立即提供给您。祝你有美好的一天。 (:
我仍然不知道是什么导致了这些错误,但经过一些更改后,我现在可以创建迁移了。以下是我所做的 3 处更改:
我安装了Microsoft.EntityFrameworkCore.Tools
我改了
services.AddDbContext<MyDBContextName>(options => options.UseSqlServer("DefaultConnection"));
为
services.AddDbContext<MyDBContextName>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
- 最后,在我的app.setting.json我改变了
"connectionStrings":
{
"DefaultConnection": "Data Source=MyName;Initial Catalog=PracticaApiSecurity;Integrated Security=True"
},
为
"connectionStrings": {
"DefaultConnections": "Data Source=MyName;Initial Catalog=PracticaApiSecurity;Integrated Security=True"
},
已更改连接 S 的连接
正在安装 Microsoft.EntityFrameworkCore.Tools 对我有用
就我而言,我已正确配置和设置所有内容,因此 none 这些建议奏效了。我进行了更多研究,发现我的问题是我还安装了 EntityFrameWork 软件包和 EntityFrameWorkCore。我将后者用于执行 SqlCommand 的库中的一些旧函数。
安装这两个包完全清理了我的项目,所以我最终不得不按照这个建议完全重置我的 EntityFramework 迁移:
Reset Entity Framework
我通过关闭我的 Visual Studio 2019 实例并重新打开它来摆脱这个错误。
运行:
PM> EntityFrameworkCore\Add-Migration
为我工作:
enter image description here
在数据包管理器控制台中选择正确的项目解决了问题。
我正在尝试在 ASP.NET 核心 Web 应用程序中创建迁移,但是当我尝试这样做时,程序包管理器控制台 returns 出现警告和错误:
警告:"A version of Entity Framework older than 6.3 is also installed. The newer tools are running. Use 'EntityFramework\Add-Migration' for the older version."
错误:"No migrations configuration type was found in the assembly 'MyProjectName'. (In Visual Studio you can use the Enable-Migrations command from Package Manager Console to add a migrations configuration)."
经过一番研究,我发现一个常见的问题是包管理器中选择的默认项目不正确;但这不是我的情况,因为它只给了我一个选择,而且这是我正在处理的项目。 我还发现当前的解决方案是使用 "Enable-Migrations" 命令启用迁移,但是当我尝试这样做时,它给了我同样的警告和另一个错误:
错误:在程序集中找不到上下文类型 'MyProjectName'。
我也以不同的方式尝试了 "enable-migraitons" 命令,"enable-migrations -contexttypename MyDBContextName",但这给了我另一个错误:
错误:在程序集 'MyProjectName' 中找不到上下文类型 'MyDBContextName'。
但我实际上有以下class:
public class MyDBContextName: IdentityDbContext<UserModel>
{
public MyDBContextName(DbContextOptions<MyDBContextName> options) : base(options)
{
}
}
我的 startup/configure 服务中有这个 class:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<MyDBContextName>(options => options.UseSqlServer("DefaultConnection"));
services.AddIdentity<UserModel, IdentityRole>().AddEntityFrameworkStores<MyDBContextName>().AddDefaultTokenProviders();
services.AddControllers();
}
最后,
知道可能导致这些错误的原因吗? 非常感谢您抽出宝贵时间,如果您需要更多信息,我会在看到您的请求后立即提供给您。祝你有美好的一天。 (:
我仍然不知道是什么导致了这些错误,但经过一些更改后,我现在可以创建迁移了。以下是我所做的 3 处更改:
我安装了Microsoft.EntityFrameworkCore.Tools
我改了
services.AddDbContext<MyDBContextName>(options => options.UseSqlServer("DefaultConnection"));
为
services.AddDbContext<MyDBContextName>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
- 最后,在我的app.setting.json我改变了
"connectionStrings":
{
"DefaultConnection": "Data Source=MyName;Initial Catalog=PracticaApiSecurity;Integrated Security=True"
},
为
"connectionStrings": {
"DefaultConnections": "Data Source=MyName;Initial Catalog=PracticaApiSecurity;Integrated Security=True"
},
已更改连接 S 的连接
正在安装 Microsoft.EntityFrameworkCore.Tools 对我有用
就我而言,我已正确配置和设置所有内容,因此 none 这些建议奏效了。我进行了更多研究,发现我的问题是我还安装了 EntityFrameWork 软件包和 EntityFrameWorkCore。我将后者用于执行 SqlCommand 的库中的一些旧函数。
安装这两个包完全清理了我的项目,所以我最终不得不按照这个建议完全重置我的 EntityFramework 迁移: Reset Entity Framework
我通过关闭我的 Visual Studio 2019 实例并重新打开它来摆脱这个错误。
运行: PM> EntityFrameworkCore\Add-Migration
为我工作:
enter image description here
在数据包管理器控制台中选择正确的项目解决了问题。