简体   繁体   中英

Code first migrations when using Filesystem based publish

I am relatively new to this. I am trying to publish my asp.net web application to a production IIS server. In visual studio I select Filesystem based Publishing, since web deploy is not supported by that server.

I use Code First Migrations for my Databases. As long as it is the first time, or if I drop all my tables, the deployments to production work fine. However if I have existing tables with production data, I get the error below -

"The model backing the 'ApplicationDbContext' context has changed since the database was created. Consider using Code First Migrations to update the database"

How do I enable automatic schema updates when my model changes? I have "AutomaticMigrationsEnabled" my Configuration.cs set to true. Do I need to set something in my global.asax or Web.Config? What am I missing?

On my local server I can run Update-Database. How do I make this automatically happen in production whenever I push model updates?


UPDATE: I was able to get rid of the error in production by following the steps in Using Entity Framework (code first) migrations in production

However I am not sure if this is the best way to fix the problem. Or if I wanted production to keep running this line every time the application starts.

Database.SetInitializer<ApplicationDbContext>(new 
MigrateDatabaseToLatestVersion<ApplicationDbContext, Configuration>());

When you use MigrateDatabaseToLatestVersion it will indeed run your migrations for you. So you definitely need that or one of the alternatives like using DbMigrator in code or generating a script ( update-database -Script ).

These will run the migrations in your folder. The problem is you have AutomaticMigrationsEnabled = true in your configuration. This tells EF to just automatically update your database and does not create the code based migration file.

There are a couple of solutions. First, you could add a manual migration before you deploy ( add-migration MyMigrationName ) or you could switch your initializer to something like CreateDatabaseIfNotExists and then call the migrations in code as shown above.

See here for a detailed explanation.

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