Reputation: 1062
I think that ef migrations must be tested. As for me integration testing will be best solution. Current solution is to apply migrations to in memory database, but problem is that i want to run down scripts of migration also.
Do yoy know how to apply migrations using c# code?
Upvotes: 2
Views: 3601
Reputation: 1735
As per https://stackoverflow.com/a/38416891/2875452. I was able to find out how to migrate to a specific migration using ef core in c# code.
context.Database.EnsureDeleted(); // ensure db is deleted on starting
context.Database.Migrate(); // run migrations
context.GetService<IMigrator>().Migrate("0"); // 0 this will rollback all migrations, you can pass in the specific migration name here to go up or down to that migration
Using command line
dotnet ef database update 0
Upvotes: -1
Reputation: 24535
In the Startup.cs Configure method, we run migrations like this (in .Net core 2.0):
using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
serviceScope.ServiceProvider.GetService<DataContext>().Database.Migrate();
}
I am not sure how you can test this. Possibly backup and restore the live database to a test one, then setup your DataContext to point to the test and run the migration there?
Upvotes: 1