简体   繁体   中英

How to generate empty migration in Entity Framework Core?

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?

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)
    {
        
    }
}

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM