【问题标题】:How to create a migration without "No migrations configuration type was found in the assembly" problem?如何在没有“在程序集中找不到迁移配置类型”问题的情况下创建迁移?
【发布时间】:2021-09-15 19:42:54
【问题描述】:

我正在尝试在 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();
}

最后, 是我的 NuGet 包的图片:

任何想法可能导致这些错误? 非常感谢您抽出宝贵时间,如果您需要更多信息,我会在看到您的请求后立即提供给您。祝你有美好的一天。 (:

【问题讨论】:

    标签: visual-studio entity-framework asp.net-core migration dbcontext


    【解决方案1】:

    我仍然不知道是什么导致了这些错误,但经过一些更改后,我现在可以创建迁移。以下是我所做的 3 项更改:

    1. 我安装了 Microsoft.EntityFrameworkCore.Tools

    2. 我变了

    services.AddDbContext&lt;MyDBContextName&gt;(options =&gt; options.UseSqlServer("DefaultConnection"));

    services.AddDbContext&lt;MyDBContextName&gt;(options =&gt; options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

    1. 最后,在我的 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

    【讨论】:

    【解决方案2】:

    运行: PM> EntityFrameworkCore\Add-Migration

    为我工作:

    enter image description here

    【讨论】:

      【解决方案3】:

      安装已安装的 Microsoft.EntityFrameworkCore.Tools 对我有用

      【讨论】:

        【解决方案4】:

        就我而言,我已经正确配置和设置了所有内容,因此这些建议都没有奏效。我进行了更多研究,发现我的问题是我还安装了 EntityFrameWork 包和 EntityFrameWorkCore。我将后者用于我的库中执行 SqlCommand 的一些旧函数。

        安装这两个软件包完全清除了我的项目,因此我最终不得不按照以下建议完全重置我的 EntityFramework 迁移: Reset Entity Framework

        【讨论】:

          【解决方案5】:

          我通过关闭我的 Visual Studio 2019 实例并重新打开它来消除此错误。

          【讨论】:

            【解决方案6】:

            在数据包管理器控制台中选择正确的项目解决了这个问题。

            【讨论】:

              猜你喜欢
              • 2019-07-29
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-08-18
              • 1970-01-01
              • 1970-01-01
              • 2019-06-25
              • 1970-01-01
              相关资源
              最近更新 更多