2017-09-16 107 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在做什么,但是当我使用缺少的迁移文件构建时,它们不包含在已编译的程序集中。这会导致应用永远不会在启动时迁移。

我手动添加迁移类在解决方案浏览,然后运行该应用程序,并施加迁移。我通过多次迁移重复了这几次,从未有过其他问题。