I use the following CLI for DB Migrations:
dotnet ef migrations add <Name-of-Migration>
dotnet ef database update
However, I am looking for a way for this to happen automatically: when a change in the Model is detected.
So far, I have been able to eliminate Step 2 by doing the following in Startup.cs:
private void SetupDatabase(IApplicationBuilder app)
{
using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
{
var context = serviceScope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
//Migate any pending changes:
context.Database.Migrate();
}
}
This migrates any pending changes created by doing:
dotnet ef migrations add <Name-of-Migration>
but it does not add migration for any changes in the model. How do I automate this migrations add
?
Updated: It is possible to move the first step of generating a migration automatically from code, a design decision that I personally do not agree with. Its still not possible in your target EF7 as I originally said (it has been removed from EF7 it seems as mentioned in this SO post as well as on this blog post by a member of Microsoft EF team mentioned in comments by Ivan), but tested in EF6 after Martin reply. The second step can be automated as you also found out already so I wont reproduce it again.
The steps for the first step are as follows (in ASP.net MVC Web Application with EF6):
Enable-Migrations –EnableAutomaticMigrations
. If your application has single DB context, it has applied the changes there too.public String TestField { get; set; }
Add-Migration
command again (hopefully, as I point out in later part of this answer), you just run Update-Database
and the DB should be updated allowing your application to run fine.Why automatic model change monitoring to generate and update database automatically might backfire sometimes (in my humble opinion and from MSDN page):
The above might not apply to everyone, but thought should share the reason why I agree with design decision of not making the migration generation and application to DB automated.
Everyone considering to enable automatic migrations should for sure read MSDN page for more examples and shortcomings.
Entity Framework 4.3 has introduced the Automated Migrations.
Although I agree with Hassan's answer stating that is might be very tricky, this option actually exists.
Here is a short resume:
enable-migrations –EnableAutomaticMigration:$true
A Configuration class will be automatically generated:
public Configuration()
{
AutomaticMigrationsEnabled = true;
//Set this parameter to true if you want to let auto-migration delete data when a property is removed from an entity.
//Not setting this will result in an exception when migration should remove a column.
AutomaticMigrationDataLossAllowed = true;
}
Set the DB Initializer in your Context class
Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyDBContext, MyConfigurationClass>("MyConnectionString"));
And there you go, change your model and see the changes...
I'm still not sure I will use it though, as I want my database to change when I say so...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With