How do I specify a schema for IdentityServer4 Entity Framework Core Migrations?(如何为标识服务器4实体框架核心迁移指定架构?)
问题描述
我使用的是IdentityServer4 2.0.2,并遵循QuickStart tutorial使用了实体框架核心。我正在尝试从默认架构(Dbo)更改为SQL Server中的自定义架构。以下代码工作正常,指示DbContext在"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中的configuration()方法初始化数据库:
var context = serviceScope.ServiceProvider.GetRequiredService<ConfigurationDbContext>();
context.Database.Migrate();
问题是表仍在dbo模式中创建。如何指示Migrate()方法(来自Microsoft.EntityFrameworkCore)使用我提供的架构?
推荐答案
我是如何让它在.Net Core1.1和标识服务器4 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
  }
为标识服务器的配置设置创建了一个类。
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
                 );
}
                        这篇关于如何为标识服务器4实体框架核心迁移指定架构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何为标识服务器4实体框架核心迁移指定架构?
				
        
 
            
        - MoreLinq maxBy vs LINQ max + where 2022-01-01
 - 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
 - C#MongoDB使用Builders查找派生对象 2022-09-04
 - 输入按键事件处理程序 2022-01-01
 - WebMatrix WebSecurity PasswordSalt 2022-01-01
 - 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
 - 如何用自己压缩一个 IEnumerable 2022-01-01
 - C# 中多线程网络服务器的模式 2022-01-01
 - 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
 - Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
 
						
						
						
						
						
				
				
				
				