Faisal
Faisal

Reputation: 109

How to change the column type in table using Entity Framework core

Hi I created my database using Entity Framework core with code first approach. Now there is a change in the column type from int to varchar/string? I can change the column from the database itself but my understanding is that it won't be a good idea and would create issues. I searched through but I didn't get my answer on the net for Entity framework core.

Upvotes: 2

Views: 8209

Answers (2)

dglozano
dglozano

Reputation: 6607

You should use EF Core migrations to update your db schema. The documentation is pretty good, so make sure to go through it.

However, this is a summary of how the process would be:

  1. Make the change in your model (which by convention will be automatically detected. Alternatively, use the Fluent API in your DB Context OnCreate method or in your EntityConfigurations).
  2. Add a migration running the following CLI command : dotnet ef migrations add SomeDescriptiveNameAboutWhatThisMigrationWillDo.
  3. A migration file with an Up and Down method will be automatically generated. The Up will be run when you apply the migration, and the Down if you ever decide to revert it . You could add changes to the automatically scaffolded migration file. Based on the code in the migration file, EF Core will then generate a SQL script and apply the changes to the DB.
  4. Once you have added (and maybe edited) the migration file, you need to apply it to the DB. You do that by running dotnet ef migrations update.
  5. EF Core tracks all applied migrations in a table in your DB called by default __EFMigrationsHistory

In your particular case of changing a column type, EF Core might try to drop the column and recreate it, which will result in data loss. If you wanna keep your data, I would recommend altering the migration script to actually split the process in two: first add a new column with the new type and a slightly different name, then write some custom SQL to migrate data from the old column to the new one, then delete the old column and finally rename the new column to the correct name. To be honest, I am not sure if there is some custom migration operation that will out of the box change the data type without data loss, there might be.

To double check if the migration will generate data loss or check if it will do what you expect it to do, you can generate the SQL script that will be used by running dotnet ef migrations script <from migration> <to migration>. After reviewing it, you can either copy/paste and run the script in your DB, or just run the command detailed in step 4 above.

Upvotes: 4

fbede
fbede

Reputation: 901

You can modify your database schema to match your domain model with the add-migration command. After changing the type of the property on your c# class from int to string, simply run

add-migration <SomeDescriptiveName>

After the creation of the migration files, you can apply them with the update-database command.

You can read more about migrations here.

Upvotes: 1

Related Questions