LJF*_*ney 5 c# asp.net-core identityserver4 ef-core-2.0
I am using IdentityServer4 2.0.2, and have followed the QuickStart tutorial to use Entity Framework Core. I am trying to change from the default schema (dbo) to a custom schema in SQL Server. The following code is working correctly, instructing the DbContexts to look for the tables in the "idsrv4" schema.
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));
});
}
Run Code Online (Sandbox Code Playgroud)
In my development environment, I am initializing the database from the Configure() method in Startup.cs with the following code:
var context = serviceScope.ServiceProvider.GetRequiredService<ConfigurationDbContext>();
context.Database.Migrate();
Run Code Online (Sandbox Code Playgroud)
The problem is that the tables are still being created in the dbo schema. How can I instruct the Migrate() method (from Microsoft.EntityFrameworkCore) to use the schema that I've provided?
这是我如何让它适用于 .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
}
Run Code Online (Sandbox Code Playgroud)
创建了一个用于 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
);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2957 次 |
最近记录: |