Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply partial migration from code?

Let's say I have 3 migrations in total: A, B and C. All migrations are built in code and executed from code (C# I mean).

I would like to apply all migrations up to given level (say B), give me back control, so I could do whatever I like outside the scope of EF/migrations/etc. When I am done with my own stuff, then execute full migration (so in this case it would mean C).

I know how to perform full migration from code :-), but how to tell EF to migrate up to given level?

Executing everything from the code (C#) is crucial here -- SQL scripts, or running external tools are no-go for me.

For curious minds: use case -- I would like to prepare tests for the last migration in between.

like image 791
astrowalker Avatar asked Jan 24 '20 08:01

astrowalker


1 Answers

According to the EF-Core documentation you should be able to run:

dotnet ef database update Migration_Name

To update the database up to a certain migration.

Running from code is a bit different. You could use context.Database.Migrate() to apply all pending migrations. Looking at the source code for this method we can see that is uses the underlying IMigrate service. I suppose you could try something like this:

_context.Database.GetService<IMigrator>().Migrate("Migration_Name");

Which should be the same as the cli command i mentioned earlier. I currently have no quick way to test this, so it might not work.

like image 131
Harjan Avatar answered Oct 21 '22 02:10

Harjan