简体   繁体   中英

Removing all automatic migrations and creating code-based migrations in their place

As you're about to learn, I'm inexperienced with building applications using ASP.NET MVC, I'm deliberately coming out of my comfort zone in order to learn.

I'm using code-first migrations with Entity Framework, and I created a Train.cs model and used the Enable-Migrations command. For some reason I thought it would be a good idea to enable automatic migrations in Migrations/Configuration.cs and I continued developing with automatic migrations enabled for a while.

Then I read somewhere that automatic migrations are generally a bad thing, so I disabled them. My Train model needed a new field called Description , so I added it. I ran Add-Migration AddDescriptionToTrains , then did Update-Database . It's throwing me back this error:

Cannot find the object "dbo.Trains" because it does not exist or you do not have permissions.

I think this is happening because I deleted the mdf file that was under App_Data in order to wipe out my database and start fresh, and it doesn't have a migration to create the Trains table before it tried to add a Description field to it.

Basically my question is, can I delete all of my migrations down to InitialCreate , and then just generate one from my model? I only have one model so it should only create one migration. I just want to create the migration from the entire model, not what it thinks has changed in the model.

In short, yes you can. In my experience, anytime you can delete the content of the _MigrationsHistory table and restart generating migrations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM