2017-09-16 83 views
0

我嘗試應用編程方式使用實體框架的核心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核心工具命令每個部署。

回答

0

新的遷移文件不被拾起,並添加到在Visual Studio MacOS的解決方案資源管理器。我原先並沒有想到這件事,因爲我認爲IDE在底層使用了dotnet build,它將提取遷移文件。我不確定IDE在做什麼,但是當我使用缺少的遷移文件構建時,它們不包含在已編譯的程序集中。這會導致應用永遠不會在啓動時遷移。

我手動添加遷移類在解決方案瀏覽,然後運行該應用程序,並施加遷移。我通過多次遷移重複了這幾次,從未有過其他問題。