【问题标题】:IdentityServer4 Error: No DbContext named 'ConfigurationDbContext' was foundIdentityServer4 错误:找不到名为“ConfigurationDbContext”的 DbContext
【发布时间】:2019-09-28 10:54:48
【问题描述】:

我正在尝试通过添加实体框架将持久性连接到我的身份服务器。目前,在尝试添加迁移时,我收到错误

找不到名为“ConfigurationDbContext”的 DbContext。

在运行迁移之前,我已将cd'd 放入我的 .csproj 文件所在的目录中,并且正在运行 dotnet ef migrations add InitConfigration -c ConfigurationDbContext -o Data/Migrations/IdentityServer/Configuration 以尝试添加迁移。

我的Startup.cs 类如下所示:

    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            services.AddIdentity<ApplicationUser, IdentityRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders();

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            var migrationsAssembly = typeof(ApplicationDbContext).GetTypeInfo().Assembly.GetName().Name;

            services.AddIdentityServer()
                .AddDeveloperSigningCredential()
                .AddAspNetIdentity<ApplicationUser>()
                .AddConfigurationStore(options =>
                {
                    options.ConfigureDbContext = builder =>
                        builder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"),
                            db => db.MigrationsAssembly(migrationsAssembly));
                })
                .AddOperationalStore(options =>
                {
                    options.ConfigureDbContext = builder =>
                        builder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"),
                            db => db.MigrationsAssembly(migrationsAssembly));
                });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseIdentityServer();

            app.UseMvc();
        }
    }

我该如何解决这个问题?

编辑:经过进一步调查,当我在生成迁移时使用--project 标志如下:dotnet ef migrations add InitConfigration -c ConfigurationDbContext -o Data/Migrations/IdentityServer/Configuration --project BleacherBuddy.IdentityServerService,我收到错误:

MSBUILD:错误 MSB1009:项目文件不存在。无法 检索项目元数据。确保它是基于 MSBuild 的 .NET Core 项目

我目前的猜测是,因为这是一个服务结构项目(无状态 .net 核心),所以构建过程在这里失败并且不允许我生成迁移。经过一些研究,我仍然不确定如何克服这个问题,或者这是否是实际问题。我将简单的身份服务器项目重新创建为 Web api(不使用服务结构),并且能够按预期生成类。感谢所有帮助。

【问题讨论】:

    标签: entity-framework .net-core identityserver4 azure-service-fabric


    【解决方案1】:
    dotnet ef migrations add InitConfigration -c ConfigurationDbContext -o Data/Migrations/IdentityServer/Configuration
    

    在此命令中,您明确要求dotnet ef 使用ConfigurationDbContext 类作为数据库上下文。你的Startup.cs 中没有它,所以我假设你在其他地方有它。在这种情况下,您需要为工具提供一个完全限定的类名,包括命名空间,因此您的命令应如下所示:

    dotnet ef migrations add InitConfigration -c Fully.Qualified.Namespaces.ConfigurationDbContext -o Data/Migrations/IdentityServer/Configuration
    

    - 将 Fully.Qualified.Namespaces. 替换为您的 ConfigurationDbContext 类的实际路径。

    更新:

    另外,由于您实际上使用ApplicationDbContext 将身份服务设置为 EF 存储,因此您可能需要在命令中使用相同的上下文:

    dotnet ef migrations add InitConfigration -c ApplicationDbContext -o Data/Migrations/IdentityServer/Configuration
    

    在这种情况下,您可能还需要在上下文类名之前指定完全限定的命名空间。

    【讨论】:

    • ConfigurationDbContext 存在于包IdentityServer4.EntityFramework.DbContexts 中,而不是我的解决方案中。我的理解是Startup.cs 中的AddConfigurationStore 部分应该允许创建数据库并允许项目找到它
    • dotnet ef migrations add InitialIdentityServerConfigurationDbMigration -c IdentityServer4.EntityFramework.DbContexts.ConfigurationDbContext -o Data/Migrations/Configuration 在使用上下文的完整命名空间时有效。
    【解决方案2】:

    这就是我所做的。安装了以下软件包:

    IdentityServer4
    IdentityServer4.AspNetIdentity
    IdentityServer4.EntityFramework
    Microsoft.AspNetCore.Identity.EntityFrameworkCore
    Microsoft.EntityFrameworkCore.Design
    Microsoft.EntityFrameworkCore.SqlServer
    

    然后,我不只是使用这些引用构建 Web 项目,而是运行该项目并停止它,然后尝试 dotnet ef database update --context ConfigurationDbContext 命令,它起作用了。

    我猜测问题可能在于生成目标文件或完整的 NuGet 包还原。

    【讨论】:

    • 运行然后停止项目对我有帮助。谢谢!
    【解决方案3】:

    我正在使用Identity Server 4。在code进行各种配置后,我访问了这个headline

    需要注意的一点从 Visual Studio 中尝试无济于事。因此,最好在命令提示符下打开您的 IdentityServer 项目目录并先尝试以下命令

    dotnet tool install --global dotnet-ef
    dotnet add package Microsoft.EntityFrameworkCore.Design
    

    然后尝试关注

    dotnet ef migrations add InitialIdentityServerPersistedGrantDbMigration -c PersistedGrantDbContext -o Migrations/IdentityServer/PersistedGrantDb
    dotnet ef migrations add InitialIdentityServerConfigurationDbMigration -c ConfigurationDbContext -o Migrations/IdentityServer/ConfigurationDb
    

    您的问题将得到解决。

    【讨论】:

      【解决方案4】:

      此问题将在三种情况下发生:

      1. 即使您将迁移放在单独的类库中,所以在这种情况下,您必须实现设计时接口并遵循 Microsoft 说明。

      2. 或者你错误地将这个 Nuget 包安装到类库中IdentityServer4.EntityFrameWork

      3. 您可能通过 Visual Studio 启用了 docker-compose,这可能会导致与我在此处描述的相同的问题:Add-Migration while using database in separate Docker container throws an error

      在所有情况下,请确保在再次运行命令之前清理并构建解决方案。

      【讨论】:

        【解决方案5】:

        添加后我遇到了完全相同的问题

        services.AddDbContext&lt;IdentityDbContext&gt;(o =&gt; { o.UseSqlServer(Configuration.GetConnectionString("IdentityConnection")); });

        当我将其注释掉时,它可以再次找到上下文。

        【讨论】:

          【解决方案6】:

          你可以试试这个,它对我有用

          1. dotnet 添加包 IdentityServer4.EntityFramework
          2. dotnet 添加包 Microsoft.EntityFrameworkCore.SqlServer

          将这行代码添加到 Startup.cs 中

          const string connectionString = @"Data Source=(LocalDb)\MSSQLLocalDB;database=IdentityServer4.Quickstart.EntityFramework-3.0.0;trusted_connection=yes;";
          
          var migrationAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;
          
          services.AddIdentityServer()
                  .AddDeveloperSigningCredential()
                  .AddInMemoryIdentityResources(Config.GetIdentityResources())
                  .AddInMemoryApiResources(Config.GetAllApiResources())
                  .AddInMemoryClients(Config.GetClients())
                  .AddTestUsers(Config.GetUsers())
                  //Configuration Store: clients and resources
                  .AddConfigurationStore(options =>
                  {
                      options.ConfigureDbContext = b =>
                          b.UseSqlServer(connectionString ,
                              sql => sql.MigrationsAssembly(migrationAssembly ));
                  })
                  //Operational Store: tokens, consents, codes, etc
                  .AddOperationalStore(options =>
                  {
                      options.ConfigureDbContext = b =>
                          b.UseSqlServer(connectionString,
                              sql => sql.MigrationsAssembly(migrationAssembly ));
                  });            
          
          

          现在,最后尝试添加迁移

          1. Add-Migration Initial_Configuration -c ConfigurationDbContext -o Migrations/IdentityServer4/ConfigurationDb

          2. 添加迁移 Initial_Persisted -c PersistedGrantDbContext -o Migrations/IdentityServer4/PersistedDb

          【讨论】:

            【解决方案7】:

            这可能会有所帮助。这解决了我的问题,因为我的项目中有 docker-compose。我不得不删除 docker-compose 并重新添加它。

            https://stackoverflow.com/a/60320410/4977086

            【讨论】:

              猜你喜欢
              • 2018-01-16
              • 2019-07-02
              • 1970-01-01
              • 1970-01-01
              • 2018-07-09
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多