我尝试应用编程方式使用实体框架的核心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核心工具命令每个部署。