2

We have to add some rows in our current database. My boss tells me to use migrations for it (we have a .net core app, and are using EF core 1.1.1). I am unsure on how to proceed, I had the understanding that migrations were a "database schema modifications" thing, not a "data update" thing. I know also seeds exist, but I think they are "first/original data filling" to be done once in the project, not at any random moment. So, should I use seedings or should I use migrations? Does migrations for simple data manipulation even exist? Or is there a third, proper way to do this?

Alejandro B.
  • 4,807
  • 2
  • 33
  • 61

2 Answers2

2

you can create your own Up/Down override and tell it to create or insert as necessary with T-SQL. It would be a migration that didn't make schema changes.. The down would remove those seeded items though.

public partial class InsertMigrationRows10 : Migration
{
    protected override void Up( MigrationBuilder migrationBuilder)
    {
        migrationBuilder.Sql("INSERT INTO SOMETHING(xx, xx, xx)", suppressTransaction: false);
    }
    protected override void Down(MigrationBuilder migrationBuilder)
    {
        //reverse what ever you did in UP
       migrationBuilder.Sql("DELETE SOMETHING ...."); 
    }
}

I think this is what you are looking for. and Update-Database (PMC) call would be necessary to get it to run. dotnet ef database update otherwise from with in the directory of the project. Of course keep mind I don't know if this is the best solution either.

mvermef
  • 3,814
  • 1
  • 23
  • 36
1

It looks like the most common approach is to use plain SQL in your Up migration - see the docs on Data Motion / Custom SQL

There are similar suggestions on stack overflow already, see Entity Framework - Migrations - Code First - Seeding per Migration and Best way to incrementally seed data in Entity Framework 4.3

When doing an initial seed, you can just override the Seed method in your Configuration.cs see here

Community
  • 1
  • 1
ironstone13
  • 3,325
  • 18
  • 24