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

时间:2019-05-10 13:56:11

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

我试图通过添加实体框架将持久性与我的身份服务器挂钩。当前,当尝试添加迁移时,我收到错误消息

  

未找到名为“ 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();
        }
    }

如何解决此问题?

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

  

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

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

6 个答案:

答案 0 :(得分:1)

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

INSERT INTO @answerTable 
SELECT T.*
from MainTable as T
WHERE 
(@notes is null OR T.nkNotes LIKE '%' + @notes + '%') and
(
(T.fkTestType in (select TestType from @tempTestType) and T.fkLocation in (select fkLocation from @tempLocation))
or 
((select count(1) where exists (select TestType from @tempTestType))=0 and T.fkLocation in (select fkLocation from @tempLocation))
or
(T.fkTestType in (select TestType from @tempTestType) and (select count(1) where exists (select fkLocation from @tempLocation))=0)
)
OPTION(RECOMPILE);

然后,我运行该项目并停止它,然后尝试使用 IdentityServer4 IdentityServer4.AspNetIdentity IdentityServer4.EntityFramework Microsoft.AspNetCore.Identity.EntityFrameworkCore Microsoft.EntityFrameworkCore.Design Microsoft.EntityFrameworkCore.SqlServer 命令成功了。

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

答案 1 :(得分:0)

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类的实际路径。

UPD:

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

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

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

答案 2 :(得分:0)

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

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

  2. 或者您正在错误地J35将此Nuget软件包安装到类库中。

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

在所有情况下,请确保先清洁然后再构建解决方案,然后再次运行命令。

答案 3 :(得分:0)

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

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

答案 4 :(得分:0)

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

services.AddDbContext<IdentityDbContext>(o => { o.UseSqlServer(Configuration.GetConnectionString("IdentityConnection")); });

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

答案 5 :(得分:0)

我正在使用 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

Command in Cmder

您的问题将得到解决。

Migrations created successfully after using cmd

相关问题