本文介绍了如何为标识服务器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实体框架核心迁移指定架构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!