Build failed when removing migrations in asp.net razor pages

92 views Asked by At

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-Migration and Update-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:

  1. Went to the code and removed that line
  2. Opened Package Manager Console and ran Update-Database - IT FAILED
  3. 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!

1

There are 1 answers

1
Lee On

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 update

Options 2: Remove And Recreate

  1. Roll back database to previous state dotnet ef database update <previousmigrationname>
  2. Remove the migration. dotnet ef migration remove
  3. Add the new migration dotnet 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.