我正在嘗試使用asp.net vnext和EF 7.0創建一個簡單的網站。對於開發,我使用localDB並且遷移工作正常。然後,我將我的站點部署到Azure,並希望將遷移應用於Azure SQL Server。
有沒有辦法將遷移應用於生產數據庫,或者如何動態更改連接字符串以進行遷移?
每次應用遷移時都不想更改連接字符串。
有三種方法可以應用遷移。
以編程方式(過時:請參閱下面的更新)
context.Database.ApplyMigrations()
從命令行。這真正意味著管理本地數據庫的開發人員,而不是遠程服務器。如您所述,針對Azure運行需要您的本地項目包含Azure的連接字符串。
生成並手動運行腳本。
$ dnx ef migrations script
(要么)
PM> Script-Migration
這些命令的參數(以及確切的命令)尚未100%設置。沒有關於遷移的官方文檔。與此同時, 此設計文檔顯示了計劃的遷移命令。
UPDATE
API現在是
context.Database.Migrate()
Startup.cs
下面的代碼將在啟動時應用數據庫遷移。
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
// other app setup omitted.
if (env.IsDevelopment())
{
// development settings here.
app.UseDeveloperExceptionPage();
}
else
{
// production settings here.
try
{
using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>()
.CreateScope())
{
serviceScope.ServiceProvider.GetService<YourDbContext>()
.Database.Migrate();
}
}
catch
{
// ignored
}
}
}