.net core ef migration testing - apply migrations using c# method

.net-core ef-migrations entity-framework-core

Question

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?

1
1
3/12/2019 1:28:47 AM

Accepted Answer

Get your context and call

context.Database.Migrate();
2
1/23/2018 6:27:33 AM

Popular Answer

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?



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow