IdentityServer4 错误:未找到名为“ConfigurationDbContext”的 DbContext

Gre*_*egH 6 entity-framework .net-core azure-service-fabric identityserver4

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

未找到名为“ConfigurationDbContext”的 DbContext。

在运行迁移之前,我已经cd进入了我的 .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();
        }
    }
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?

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

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

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

Ser*_*sev 7

dotnet ef migrations add InitConfigration -c ConfigurationDbContext -o Data/Migrations/IdentityServer/Configuration
Run Code Online (Sandbox Code Playgroud)

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

dotnet ef migrations add InitConfigration -c Fully.Qualified.Namespaces.ConfigurationDbContext -o Data/Migrations/IdentityServer/Configuration
Run Code Online (Sandbox Code Playgroud)

- 替换Fully.Qualified.Namespaces.为您班级的实际路径ConfigurationDbContext

更新:

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

dotnet ef migrations add InitConfigration -c ApplicationDbContext -o Data/Migrations/IdentityServer/Configuration
Run Code Online (Sandbox Code Playgroud)

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

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