当您说“基础数据”时,我假设您的意思是为每个环境播种。迁移提供了播种机制 https://www.asp.net/web-api/overview/data/using-web-api-with-entity-framework/part-3为了这。在 Seed() 中,您可以像在常规代码中一样区分环境。我们喜欢使用 Web.config 转换设置:
protected override void Seed(BookService.Models.BookServiceContext context)
{
if (ConfigurationManager.AppSettings["DeployEnvironment"] == "UAT")
{
context.Authors.AddOrUpdate(x => x.Id,
new Author() { Id = 1, Name = "Test User" },
);
}
else if (ConfigurationManager.AppSettings["DeployEnvironment"] == "PROD")
{
context.Authors.AddOrUpdate(x => x.Id,
new Author() { Id = 1, Name = "Production User" },
);
}
}
另一种选择是编译器指令:
protected override void Seed(BookService.Models.BookServiceContext context)
{
#if DEBUG
context.Authors.AddOrUpdate(x => x.Id,
new Author() { Id = 1, Name = "Test User" },
);
#else
context.Authors.AddOrUpdate(x => x.Id,
new Author() { Id = 1, Name = "Production User" },
);
#endif
}
就应用迁移本身而言,我们遵循这个流程 https://msdn.microsoft.com/en-US/data/dn481501开发中。当我们准备好部署到 UAT 时,我们只需将连接字符串指向 UAT 并运行迁移,或者我们也可以创建一个脚本 http://bartwullems.blogspot.com/2013/05/generate-sql-script-from-entity.html更新数据库。在 PROD 中,我们做.