Home > Back-end >  How to generate empty migration in Entity Framework Core?
How to generate empty migration in Entity Framework Core?

Time:10-09

I am using EF Core migrations to upgrade my database schema and my data.

As I write new C# code, I need to change the data in my different database environments: development, test, industrialisation, staging and production.

Because I don't want to execute a SQL script in as many environments I have, my goal is to put some data migration in a C# migration. Then the data migration will be executed with the other schemas migrations. Preventing me from forgetting.

How can I create an empty C# migration script to change my data with calling SQL, even if I have no database change?

CodePudding user response:

You can create an empty migration with the following command in the Package Manager Console:

Add-Migration '[Insert name]'

Your new migration file will look like this:

public partial class [Insert name]: Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        
    }
}

CodePudding user response:

Without any changes in data models, when you run the below command, there will be an empty migration file. Then you can put the data manipulation sql code in Up method and revert sql code in Down method :

cli :

dotnet ef migrations add [migrationName]

package manager console :

Add-Migration [migrationName]
  • Related