我正在使用IdentityServer4 2.0.2,并按照QuickStart教程使用Entity Framework Core。我试图将默认模式(dbo)更改为SQL Server中的自定义模式。以下代码正常工作,指示DbContext在“idsrv4”模式中查找表。
在我的开发环境中,我正在使用Startup.cs文件中的Configure()方法初始化数据库,以下是代码:
问题在于表仍然在dbo模式中创建。我该如何指示Migrate()方法(来自Microsoft.EntityFrameworkCore)使用我提供的模式?
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)使用我提供的模式?
Migrate
将上下文中的任何待处理迁移应用于数据库。如果数据库不存在,将创建它。这让我想到您必须在某个地方有迁移类已经设置为使用dbo
。 - Kirk Larkindotnet ef migrations add ...
命令。由于我在Startup.Configure()中调用了context.Database.Migrate(),所以这个过程失败了。为了成功重新创建迁移,我不得不注释掉对Migrate()的调用。一旦重新创建,它们就具有预期的模式。然后我可以取消对Migrate()的注释并运行我的应用程序。 - LJFraney