I'm looking for any way to generate and run EF Core migrations on app startup/at runtime. I'm developing in a docker container, so I do not have the luxury of using the migrations cli when my schema changes I want to check if the current database lines up with my schema, generate and run the migrations if not, and carry on, without having to completely recreate the database if it exists (EnsureDeleted
/EnsureCreated
).
Essentially, I'm looking for a way to call dotnet ef migrations add <migration name>
and dotnet ef migrations update
in my C# code (in the Startup.Configure
method, if possible), if the schema in code does not line up with the database schema. I've seen the Migrate
method, but that only applies pending migrations, it does not generate anything. And I cannot use EnsureDeleted
and EnsureCreated
because they would remove the entire database, even if there is some preexisting data.
I'm using .Net 6 with an ASP.Net Web Api project, if it helps.