未应用迁移
Migrations are not being applied
我正在尝试在 Code-First ASP.Net Core 2.0 项目中使用 Entity Framework Core 2.0 以编程方式应用迁移。如果我 运行 通过终端手动迁移,则可以毫无问题地应用它们。尽管在我的 Startup
class 中应用迁移导致数据库模型永远不会改变。
我做错了吗?
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 migrations add FourthMigration --context EFCore_Test.DataAccess.ApplicationContext
我有多个 DataContext
类型;只有一个代表整个数据模型,其余的仅用于在更特定领域的庄园中访问数据库。 ApplicationContext
代表我的 "everything + kitchen sink" 数据上下文。我执行迁移和更新时使用的就是这个上下文。
在准备部署到 Azure 时,我想让 Web 应用程序在每次部署时自行迁移,而不必将 powershell 脚本连接到 运行 dotnet 核心工具命令。
新的迁移文件未被拾取并添加到 Visual Studio for macOS 的解决方案资源管理器中。我最初对此没有任何想法,因为我假设 IDE 在后台使用 dotnet build
,它将获取迁移文件。我不确定 IDE 的作用,但是当我在缺少这些迁移文件的情况下进行构建时,它们不会包含在已编译的程序集中。这会导致应用程序在启动时永远不会迁移。
我在解决方案资源管理器中手动添加了迁移 类,然后 运行 应用程序和迁移被应用。我通过多次迁移重复了几次,再也没有遇到其他问题。
我正在尝试在 Code-First ASP.Net Core 2.0 项目中使用 Entity Framework Core 2.0 以编程方式应用迁移。如果我 运行 通过终端手动迁移,则可以毫无问题地应用它们。尽管在我的 Startup
class 中应用迁移导致数据库模型永远不会改变。
我做错了吗?
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 migrations add FourthMigration --context EFCore_Test.DataAccess.ApplicationContext
我有多个 DataContext
类型;只有一个代表整个数据模型,其余的仅用于在更特定领域的庄园中访问数据库。 ApplicationContext
代表我的 "everything + kitchen sink" 数据上下文。我执行迁移和更新时使用的就是这个上下文。
在准备部署到 Azure 时,我想让 Web 应用程序在每次部署时自行迁移,而不必将 powershell 脚本连接到 运行 dotnet 核心工具命令。
新的迁移文件未被拾取并添加到 Visual Studio for macOS 的解决方案资源管理器中。我最初对此没有任何想法,因为我假设 IDE 在后台使用 dotnet build
,它将获取迁移文件。我不确定 IDE 的作用,但是当我在缺少这些迁移文件的情况下进行构建时,它们不会包含在已编译的程序集中。这会导致应用程序在启动时永远不会迁移。
我在解决方案资源管理器中手动添加了迁移 类,然后 运行 应用程序和迁移被应用。我通过多次迁移重复了几次,再也没有遇到其他问题。