Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change or rename a column name without losing data with Entity Framework Core 2.0

I realised that I had spelt one of my column headers incorrectly so I changed it in the model and created a new migration to update it into the database. All worked perfectly until I realised that what actually appeared to happen was a new column replaced the existing column and erased all the data. As it happens, as this was a tutorial database, it was no big deal and a work of a few minutes to put the data back.

How/what do I do to update/rename a column without losing the data in it?

Not sure how this didn't come up in my search but here is a directly related post: Rename table field without losing data, using automatic migrations

like image 823
nathanjw Avatar asked May 04 '18 15:05

nathanjw


People also ask

How do I rename a column first in Entity Framework?

For EF core you can use like this, migrationBuilder. RenameColumn(name: "oldname",table: "tablename",newName: "newname",schema: "schema"); docs.microsoft.com/en-us/dotnet/api/…

How do I rename a column in R studio?

To rename a column in R you can use the rename() function from dplyr. For example, if you want to rename the column “A” to “B”, again, you can run the following code: rename(dataframe, B = A) .


Video Answer


2 Answers

EF Core creates its migrations by comparing your models to the current database snapshot (a c# class). It then uses this to create a migration file you can review. However, EF Core cannot always know if you replaced this column or created a new column. When you check your migration file, make sure there are no column drops, index drops (related to this column) etc. You can replace all these with something like this:

migrationBuilder.RenameColumn(
    name: "ColumnA",
    table: "MyTable",
    newName: "ColumnB");
like image 195
Neville Nazerane Avatar answered Oct 17 '22 06:10

Neville Nazerane


migrationBuilder.RenameColumn usually works fine but sometimes you have to handle indexes as well.

migrationBuilder.RenameColumn(name: "Identifier", table: "Questions", newName: "ChangedIdentifier", schema: "dbo");

Example error message when updating database:

Microsoft.Data.SqlClient.SqlException (0x80131904): The index 'IX_Questions_Identifier' is dependent on column 'Identifier'.

The index 'IX_Questions_Identifier' is dependent on column 'Identifier'.

RENAME COLUMN Identifier failed because one or more objects access this column.

In this case you have to do the rename like this:

migrationBuilder.DropIndex(
    name: "IX_Questions_Identifier",
    table: "Questions");

migrationBuilder.RenameColumn(name: "Identifier", table: "Questions", newName: "ChangedIdentifier", schema: "dbo");

migrationBuilder.CreateIndex(
    name: "IX_Questions_ChangedIdentifier",
    table: "Questions",
    column: "ChangedIdentifier",
    unique: true,
    filter: "[ChangedIdentifier] IS NOT NULL");
like image 3
Ogglas Avatar answered Oct 17 '22 07:10

Ogglas