如何为IdentityServer4实体框架核心迁移指定架构?

时间:2017-12-01 17:21:12

标签: c# asp.net-core identityserver4 ef-core-2.0

我使用的是IdentityServer4 2.0.2,并遵循QuickStart tutorial使用Entity Framework Core。我试图从默认架构(dbo)更改为SQL Server中的自定义架构。以下代码正常工作,指示DbContexts在" idsrv4"中查找表。架构。

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();

    var identityConnectionString = Configuration.GetConnectionString("Identity");
    var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;

    services.AddIdentityServer()
        .AddDeveloperSigningCredential()
        .AddTestUsers(Config.GetUsers())
        .AddConfigurationStore(options =>
        {
            options.DefaultSchema = "idsrv4";
            options.ConfigureDbContext = builder => builder.UseSqlServer(identityConnectionString,
                sql => sql.MigrationsAssembly(migrationsAssembly));

        })
        .AddOperationalStore(options =>
        {
            options.DefaultSchema = "idsrv4";
            options.ConfigureDbContext = builder => builder.UseSqlServer(identityConnectionString, 
                sql => sql.MigrationsAssembly(migrationsAssembly));                    
        });
}

在我的开发环境中,我使用以下代码从Startup.cs中的Configure()方法初始化数据库:

var context = serviceScope.ServiceProvider.GetRequiredService<ConfigurationDbContext>();
context.Database.Migrate();

问题是仍然在dbo架构中创建表。如何指示Migrate()方法(来自Microsoft.EntityFrameworkCore)使用我提供的架构?

1 个答案:

答案 0 :(得分:0)

以下是我如何使用.Net Core 1.1和IdentityServer4 1.x

为appsettings.json添加了我的配置部分。

"IdentityServerConfig": {
    "DBConfig": {
      "IdentityServer": "Data Source=dbservername.database.windows.net;User ID=user_Name;Password=password;Initial Catalog=DBNAme;",
      "DefaultSchema": "IDSVR"
    },
    "CertificateConfig": {
      "Thumbprint": "",
      "FileName": "cert.pfx",
      "SubDirectory": "",
      "PassPhrase": "password"
    },
    "RaiseErrorEvents": true,
    "RaiseFailureEvents": true,
    "RaiseInformationEvents": true,
    "RaiseSuccessEvents": true
  }

为IdentityServer创建了一个配置设置类。

public class IdentityServerConfig
    {
        public CertificateConfig CertificateConfig { get; set; }
        public DBConfig DBConfig { get; set; }

        public bool RaiseErrorEvents { get; set; }
        public bool RaiseFailureEvents { get; set; }
        public bool RaiseInformationEvents { get; set; }
        public bool RaiseSuccessEvents { get; set; }
    }

public void ConfigureServices(IServiceCollection services)
{
     ...Other Code

     var identityServerConfig = config.GetSection("IdentityServerConfig").Get<IdentityServerConfig>();


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


    builder = services.AddIdentityServer(options =>
            {
                options.Events.RaiseErrorEvents = identityServerConfig.RaiseErrorEvents ;
                options.Events.RaiseFailureEvents = identityServerConfig.RaiseFailureEvents ;
                options.Events.RaiseInformationEvents = identityServerConfig.RaiseInformationEvents ;
                options.Events.RaiseSuccessEvents = identityServerConfig.RaiseSuccessEvents ;
            })

     .AddConfigurationStore(
                     b => b.UseSqlServer(identityServerConfig.DBConfig.IdentityServer ,
                     options =>
                     {
                         options.MigrationsAssembly(migrationsAssembly);
                     }), storeOption =>
                     {
                         storeOption.DefaultSchema = identityServerConfig.DBConfig.DefaultSchema ;//IDSVR
                     })
     .AddOperationalStore(
                 b => b.UseSqlServer(identityServerConfig.DBConfig.IdentityServer,
                 options => options.MigrationsAssembly(migrationsAssembly)
                 ), storeOption => storeOption.DefaultSchema = identityServerConfig.DBConfig.DefaultSchema //IDSVR
                 );
}