'Entity Framework Core Runtime database Creation and Migration inside WPF
I have an issue with migrations in EF Core. From what I understand Migration allows me to modify/update database model with the ability to move between old and newer versions of the database. The problem is My WPF app Creates database on the fly. I'm able to create Database ('Code First Approach') and the migration Table is there: enter image description here
So my question is Can I do something like Add-Migration / Update-Database from C# code. So when somethings happen in the future for example I will have to add some columns or tables - I can update my app and my app will handle migration. Is there a way?
Solution 1:[1]
I am not familiar with WPF applications, but with a C# API I was able to add that functionality at startup. context.Database.Migrate() is the command to apply migrations programmatically.
This is the function that I used in my API Startup file:
private void UpgradeDatabase(IApplicationBuilder app)
{
using (var serviceScope = app.ApplicationServices.CreateScope())
{
var context = serviceScope.ServiceProvider.GetService<PrimeDbContext>();
if (context != null && context.Database != null)
{
context.Database.Migrate();
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Brook Stang |
