I want to remove some unnecessary fields added in my class model without starting the project from scratch
Context:
- I am creating a website using RazorPages.
- I used the code-first approach and started by creating classes of my model.
- I added new scaffolded item for each table to create the pages
- Then Ran
Add-MigrationandUpdate-Database - When I ran the app it went well but I saw that I added some fields that I want removed: e.g.,
public class Animal
{
[Key]
public int id {get; set;}
public string? surname {get; set;} // I WANT THIS ONE REMOVED
}
What I did:
- Went to the code and removed that line
- Opened Package Manager Console and ran
Update-Database- IT FAILED - Figured I need to scrap the Migration and run
Remove-Migration- Which also failed.
The Error insists that the field I had deleted is missing, which is my intention.
ErrorMessage
Error CS1061 'X' does not contain a definition for 'X_Field_1' and no accessible extension method 'X_Field_1' accepting a first argument of type 'X' could be found (are you missing a using directive or an assembly reference?)
Goal: I would like to remove just the field and make the changes to my database then run my web pages without scrapping my folders - scaffolding takes too long on my pc!
The way I see it you have 3 options here.
Option 1: Start Again
If it's really early in your development cycle (And from the look of this code it is) Simply Delete your DB delete all your migrations and snapshot and start again with a new initial migration.
dotnet ef add-migration initial,dotnet ef database updateOptions 2: Remove And Recreate
dotnet ef database update <previousmigrationname>dotnet ef migration removedotnet ef migration add <newmigrationname>Option 3: Plough Ahead
Instead of rolling back the migration, once you've changed the model simply add a new migration to remove the columns you don't want and update the database.