May be i'm missing something very obvious. But i haven't been able to figure out a way to add a new column to an existing table/model in EF Core.
This is the documentation that I'm following: https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/powershell
And this is what i have done so far:
- Created migration using this command: "Add-Migration -Name CodingSoldierDbContextMigration -OutputDir Migrations -Context CodingSoldierDbContext"
- Updated database using the command: "Update-Database -Migration CodingSoldierDbContextMigration -Context CodingSoldierDbContext". Tables got created in the Database.
- Now i need to add a new column to an existing table. I add that column to the model in the .cs file. And i remove the existing migration: "Remove-Migration -Force -Context CodingSoldierDbContext"
- Now i re-run the commands in steps 1 and 2. Add-Migration works and migration gets created. But Update-Database fails with the error: "There is already an object named 'AspNetRoles' in the database." which means the table is already present in the database which makes sense.
So how do i update an already existing table table ?
2 ways i can think of are:
- Drop the database. Create migration and update database. But all data will be gone.
- Add column in the model. Manually update the Table using a SQL script to add the new column.
But i feel there should be a better way to do this.