Entity framework IdentityServer4错误:没有名为';ConfigurationDbContext';被发现

Entity framework IdentityServer4错误:没有名为';ConfigurationDbContext';被发现,entity-framework,.net-core,identityserver4,azure-service-fabric,Entity Framework,.net Core,Identityserver4,Azure Service Fabric,我正试图通过添加实体框架将持久性连接到我的标识服务器。目前,在尝试添加迁移时,我收到了错误消息 未找到名为“ConfigurationDbContext”的DbContext 在运行迁移之前,我将cd'd放入我的.csproj文件所在的目录,并正在运行dotnet ef migrations add InitConfigration-c ConfigurationDbContext-o Data/migrations/IdentityServer/Configuration,以尝试添加迁移 我的

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

未找到名为“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();
        }
    }
公共类启动
{
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
public void配置服务(IServiceCollection服务)
{
services.AddDbContext(选项=>
options.UseSqlServer(Configuration.GetConnectionString(“DefaultConnection”));
服务.额外性()
.AddEntityFrameworkStores()
.AddDefaultTokenProviders();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
var migrationassembly=typeof(ApplicationDbContext.GetTypeInfo().Assembly.GetName().Name;
services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddAsNetIdentity()
.AddConfigurationStore(选项=>
{
options.ConfigureDbContext=builder=>
builder.UseSqlServer(Configuration.GetConnectionString(“DefaultConnection”),
db=>db.migrationassembly(migrationassembly));
})
.addStore(选项=>
{
options.ConfigureDbContext=builder=>
builder.UseSqlServer(Configuration.GetConnectionString(“DefaultConnection”),
db=>db.migrationassembly(migrationassembly));
});
}
//此方法由运行时调用。请使用此方法配置HTTP请求管道。
公共无效配置(IApplicationBuilder应用程序,IHostingEnvironment环境)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseIdentityServer();
app.UseMvc();
}
}
我如何解决这个问题


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

MSBUILD:错误MSB1009:项目文件不存在。不能 检索项目元数据。确保它是基于MSBuild的.NET核心 计划

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

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
-将
完全.Qualified.namespace.
替换为
ConfigurationDbContext
类的实际路径

UPD:

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

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

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

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

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

  • 或者您错误地将此Nuget包安装到类库
    IdentityServer4.EntityFrameWork

  • 您可以通过visual studio启用docker compose,这可能会导致与我在此处所述相同的问题:


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

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


    以下是我所做的。已安装以下软件包:

    IdentityServer4
    IdentityServer4.AspNetIdentity
    IdentityServer4.EntityFramework
    Microsoft.AspNetCore.Identity.EntityFrameworkCore
    Microsoft.EntityFrameworkCore.Design
    Microsoft.EntityFrameworkCore.SqlServer
    
    然后,我运行项目并停止它,然后尝试
    dotnet ef数据库更新--context-ConfigurationDbContext
    命令,它成功了


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

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

    services.AddDbContext(o=>{o.UseSqlServer(Configuration.GetConnectionString(“IdentityConnection”);})

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

    我正在使用。在中进行了各种配置之后,我访问了这个

    需要注意的一点是,从VisualStudio尝试不会有帮助。因此,最好打开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