What is the proper way to fix data on the database using .net Core?

ef-migrations entity-framework entity-framework-core seeding

Question

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?

1
2
5/5/2017 6:08:43 PM

Accepted Answer

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

1
5/23/2017 12:26:06 PM

Popular Answer

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.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow