Update data in existing column with EF Core Migration

asp.net-core database-migration ef-migrations entity-framework-core

Question

My table in SQL has two fields: Id and Status. It looks something like this

ID | Status

1 | "Status1"

2 | "Status2"

I should make a migration that will change those status values into those that I want? How can I achieve this?

1
0
2/19/2020 12:53:24 PM

Accepted Answer

I should make a migration that will change those status values into those that I want?

Try add SQL statements into Up method of the generated migration file manually like below

protected override void Up(MigrationBuilder migrationBuilder)
{
        migrationBuilder.Sql("UPDATE A SET AName = 'Jhon' WHERE Id=3");
}

For updating multiple records , you could refer to the following code

protected override void Up(MigrationBuilder migrationBuilder)
{
        migrationBuilder.Sql(
           "UPDATE A SET AName = CASE Id " +
                   "WHEN 1 THEN 'Shariy' " +
                   "WHEN 2 THEN 'Mary'" +
                   "ELSE AName END " +
                   "WHERE Id IN(1,2)");
}
1
2/20/2020 7:27:20 AM


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