Reputation: 1103
How can I read migrations files at runtime and apply them to my database in EF Core?
For tests I generate my migrations files before running the tests. Now I would like to take the migrations files (I know the path to where they are generated) and apply them at runtime. How could I do that?
I know that the dbContext.Database.Migrate();
should apply all the migrations, but the application does not happen. I think that the Migrate()
method does not know where to look for the migrations files, so I need a way to tell him that. But I can not find an example code for this.
Upvotes: 0
Views: 1109
Reputation: 683
Hi I think this is what you are looking for: https://learn.microsoft.com/en-us/ef/core/managing-schemas/migrations/projects?tabs=dotnet-core-cli
options.UseSqlServer(
connectionString,
x => x.MigrationsAssembly("MyApp.Migrations"));
using (var scope = host.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
db.Database.Migrate();
}
Upvotes: 1