问题
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.
回答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
.
来源:https://stackoverflow.com/questions/26074330/second-attempt-to-generate-script-sql-script-did-not-work