我嘗試應用編程方式使用實體框架的核心2.0遷移,在代碼優先ASP.Net 2.0的核心項目。如果我通過終端手動運行遷移,它們將毫無問題地應用。儘管在我的Startup
類中應用遷移,但數據庫模型不會改變。遷移不被應用於
我這樣做不對嗎?
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddEntityFrameworkSqlite();
services.AddDbContext<ApplicationContext>(options => options.UseSqlite("Data Source=blogging.db"));
services.AddDbContext<UserContext>(options => options.UseSqlite("Data Source=blogging.db"));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
var services = app.ApplicationServices.GetService<IServiceScopeFactory>();
var context = services.CreateScope().ServiceProvider.GetRequiredService<ApplicationContext>();
context.Database.Migrate();
}
我可以從終端運行這個工作得很好:
DOTNET EF migration增加FourthMigration --context EFCore_Test.DataAccess.ApplicationContext
我有多個DataContext
類型;只有一個代表整個數據模型,其餘的僅用於訪問更具領域特定領域的數據庫。 ApplicationContext
代表我的「一切+廚房水槽」數據上下文。這是我執行我的遷移和更新的上下文。
在部署到Azure的準備,我想有在web-app本身遷移,而不必線了PowerShell腳本運行DOTNET核心工具命令每個部署。