2

I know that in ef core you can generate migration script from one to next migrations. Lets say i have 5 migrations:

  1. Migration1
  2. Migration2
  3. Migration3
  4. Migration4
  5. Migration5

Migration3 and Migration4 already applied to database, and i want to generate script for Migrations 1, 2 and 5. Is it possible by using ef core tools? Or maybe there is some other tools?

2 Answers 2

2

If I correctly understood your problem you want to deploy migrations to a database? You can use .Net Core CLI for that:

dotnet ef migrations script 

This will generate a SQL script from this migration to the latest migration

dotnet ef migrations script xxx_name 

You can also generate a SQL script from migration to the specified migration.

dotnet ef migrations script from_migration to_migration 
Sign up to request clarification or add additional context in comments.

Comments

0

Each migration builds on the previous and is generated assuming everything that came before it is already there. It would be very dangerous to apply the migrations out of order, or use something other than EF core to modify the portion of your database you are using it to manage. That being said, you can use the dotnet ef migrations script command to generate a script moving from one migration to another. It will include all the migrations between them as well:

dotnet ef migrations script <FromMigration> <ToMigration> -o "FileName"

2 Comments

So if I understand you're using dotnet CLI so that doesn"t mean the one I put in my answer are incorrect. The command lines in my sample are for PMC (Package Manage Console) and there it is recommanded to use the powershell one. If you're out of Visual Studio then use the dotnet CLI. Thanks a lot BTW for the downvote ;-)
yes, but sometimes it happens, and if you call context.Database.Migrate() only missed migrations will be apply to database, but in case if you generate migration script, it is impossible

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.