1
我目前正在构建一个使用迁移的.NET核心标识服务器应用程序,并想知道种什么数据库的最佳方法是关于.NET核心。在.Net核心中播种
目前我有一个DbInitializer类,在启动时调用(见下文),但想知道如果我应该做这个迁移?
public static void Seed(IApplicationBuilder applicationBuilder)
{
ConfigurationDbContext context =
applicationBuilder.ApplicationServices.GetRequiredService<ConfigurationDbContext>();
IConfiguration oConfig =
applicationBuilder.ApplicationServices.GetRequiredService<IConfiguration>();
string sSeedingConfig = GetSeedingConfiguration(oConfig);
SeedConfigurationDb(ref context, sSeedingConfig);
}
private static void SeedConfigurationDb(ref ConfigurationDbContext oContext, string sSeedingConfig)
{
if (!oContext.ApiResources.Any())
{
var oApis = GetSeedingConfigElements(sSeedingConfig, "Apis");
List<ApiResource> lApis = null;
if (oApis != null)
{
lApis = JsonConvert.DeserializeObject<List<ApiResource>>(oApis.ToString());
}
if (lApis != null)
{
foreach (var api in lApis)
{
oContext.ApiResources.Add(api.ToEntity());
}
}
}
if (!oContext.Clients.Any())
{
var oClients = GetSeedingConfigElements(sSeedingConfig, "Clients");
List<Client> lClients = null;
if (oClients != null)
{
lClients = JsonConvert.DeserializeObject<List<Client>>(oClients.ToString());
}
if (lClients != null)
{
foreach (var client in lClients)
{
oContext.Clients.Add(client.ToEntity());
}
}
}
if (!oContext.IdentityResources.Any())
{
var oIdentityResources = GetSeedingConfigElements(sSeedingConfig, "IdentityResources");
List<IdentityResource> lIdentityResources = null;
if (oIdentityResources != null)
{
lIdentityResources = JsonConvert.DeserializeObject<List<IdentityResource>>(oIdentityResources.ToString());
}
if (lIdentityResources != null)
{
foreach (var identityresource in lIdentityResources)
{
oContext.IdentityResources.Add(identityresource.ToEntity());
}
}
}
oContext.SaveChanges();
}
我实际上运行了下面的命令来创建我的数据库'dotnet ef migrations add InitialIdentityServerMigration -c ConfigurationDbContext'。这个方法默认没有构造一个配置类,所以我不知道这个实现是否与新的框架稍有不同。我知道在以前的框架版本中,这总是进行播种过程的方式,但.NET核心需要一些时间来适应。 – GSkidmore
@GSkidmore看看这个:http://www.dalsoft.co.uk/blog/index.php/2016/12/13/entity-framework-core-seeding-using-migrations/,这是一个工作,以种子与迁移,直到EF核心增加功能,这应该做的伎俩。 – TanguyB
非常感谢你,真的很感激! – GSkidmore