1

I am developing an application in ASP.NET MVC 5 and Entity Framework with a code-first approach.

Firstly, I had one class in my data context file and everything worked. enable-migrations, add-migration and update-database were working fine but second time, I added more classes and tried to execute these statements:

enable-migrations -ContextTypeName IdentityDb -MigrationsDirectory DAL\IdentityMigrations

enable-migrations -ContextTypeName SMSContext -MigrationsDirectory DAL\SMSMigrations

add-migration -ConfigurationTypeName SMSApp.DAL.IdentityMigrations.Configuration "InitialCreate"

I get an exception:

A previous migration called 'InitialCreate' was already applied to the target database. If you meant to re-scaffold 'InitialCreate', revert it by running 'Update-Database -TargetMigration $InitialDatabase', then delete '201409261933262_InitialCreate1.cs' and run 'Add-Migration InitialCreate' again.

2
  • That actually seems like a fairly straightforward exception. is there some reason you can't do what it advises you to do? Commented Sep 27, 2014 at 11:51
  • thanks Ann L. please my complete post at here. stackoverflow.com/questions/26067259/… Commented Sep 27, 2014 at 13:01

1 Answer 1

1

In Solution Explorer click icon Show All Files. Than go to App_Data, right click on mdf file and open.

In Server Explorer click on connections and right click on your connection then close it, then right click again then delete it, then delete the mdf file from the Solution explorer(this will move it to the trash bin, it is not lost yet if you decide to get it back). Then in power shell package console update-database.

NOTICE: This will totally rebuild your database inserting records from Configuration/Migrations.cs Seed method, you will lose other data. Also find AutomaticMigrations in the code and set it to true.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Yoda. i am using Mysql provider. In case of MYSQL connector. should i require to delete the mysql database?
Thanks Yoda, my problem is solved. thanks for your advice Yoda. It worked in my case. How can i mark an answer to your reply or give remarks?
@maifs You can accept my answer by the ✓ (tick button) on the left side of my answer and when your reputation will grow above 15 points you can vote up my answer. Then future viewers will know my answer worked.
I am still confused about one thing that how and in which order should we execute the commands in project manager console when a new model and modify the existing model requires to update the database. 1). Enable-migrations 2) add-migration 3). Update-database I am presuming that on the base of model changes or new model, we require these commands to be executed in the mentioned order each time. Kindly guide me in this regard.
One thing more, please reply to my this post as well so that i mark the answer. stackoverflow.com/questions/26067259/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.