How to clear all pending migrations



During my internship, I worked in a project which uses ASP.NET Web API with Entity framework. At first, I was not familiar with the Web API or Entity framework, so I got many issues. One of the first issues that I have faced is, an error occurs when I try to clear all pending migration. So, today I'm going to show you how to solve that issue.

When you change your attributes in objects in your code, you have to migrate them to the database (when you have modified the database) since the database doesn't contain the new changes. So when you try to migrate the console will show you the following error. 

Error:

“Cannot find the object “dbo.AspNetUsers” because it does not exist or you do not have permissions”

“Unable to generate an explicit migration because the following explicit migrations are pending: [*******]. Apply the pending explicit migrations before attempting to generate a new explicit migration.”

“System.InvalidOperationException: Migrations is enabled for context ‘ApplicationDbContext’ but the database does not exist or contains no mapped tables. Use Migrations to create the database and its tables, for example by running the ‘Update-Database’ command from the Package Manager Console.”


Apart from that, These kinds of errors occur when you run your project before adding migrations, when you made changes in the connection string, etc.  So you can clear existing migrations and create a new one. 

You can follow these steps: 
  • Delete the “Migrations” folder in your project.
  • Clean the solution.
  • Then in the Package Manager Console, execute the following commands one by one:
  • Enable-migrations
  • Add-migration “initial”
  • update-database

Now Build your project and go check in SQL Server Management Studio. Now you can find your new database with new changes.


Kalpani

Comments