简体   繁体   English

使用基于文件系统的发布时代码首次迁移

[英]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. 我试图将我的asp.net Web应用程序发布到生产IIS服务器。 In visual studio I select Filesystem based Publishing, since web deploy is not supported by that server. 在visual studio中,我选择基于Filesystem的发布,因为该服务器不支持Web部署。

I use Code First Migrations for my Databases. 我为我的数据库使用Code First Migrations。 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" “自创建数据库以来,支持'ApplicationDbContext'上下文的模型已发生更改。请考虑使用Code First Migrations更新数据库”

How do I enable automatic schema updates when my model changes? 我的模型更改时如何启用自动架构更新? I have "AutomaticMigrationsEnabled" my Configuration.cs set to true. 我将“AutomaticMigrationsEnabled”我的Configuration.cs设置为true。 Do I need to set something in my global.asax or Web.Config? 我需要在global.asax或Web.Config中设置一些内容吗? What am I missing? 我错过了什么?

On my local server I can run Update-Database. 在我的本地服务器上,我可以运行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. 当您使用MigrateDatabaseToLatestVersion时,它确实会为您运行迁移。 So you definitely need that or one of the alternatives like using DbMigrator in code or generating a script ( update-database -Script ). 所以你肯定需要这个或其中一个替代方案,比如在代码中使用DbMigrator或生成脚本( update-database -Script )。

These will run the migrations in your folder. 这些将在您的文件夹中运行迁移。 The problem is you have AutomaticMigrationsEnabled = true in your configuration. 问题是您的配置中有AutomaticMigrationsEnabled = true This tells EF to just automatically update your database and does not create the code based migration file. 这告诉EF只是自动更新数据库,而不是创建基于代码的迁移文件。

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. 首先,您可以在部署之前添加手动迁移( add-migration MyMigrationName ),也可以将初始化程序切换为类似CreateDatabaseIfNotExists ,然后在代码中调用迁移,如上所示。

See here for a detailed explanation. 请参阅此处获取详细说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用WebDeploy发布Web应用程序时,如何在DB上自动运行Code First Migrations - How to automatically run Code First Migrations on DB when using WebDeploy to publish web app 不使用代码优先迁移 - Not using code first migrations 如何使用Visual Studio实体框架代码优先迁移将发布部署到mysql数据库 - How to deploy publish to mysql database using visual studio entity framework code first migrations 使用代码优先迁移进行批量插入以获取天蓝色 - Bulk insert using code first migrations for azure 如何在使用基于代码的迁移时停止添加迁移检查我的数据库没有挂起的迁移? - How can I stop Add-Migration checking my database has no pending migrations when using Code-Based migrations? 使用EF(5)代码优先迁移时如何执行附加的数据库初始化 - How can I perform additional DB initialization when using EF (5) Code First Migrations 代码优先迁移不起作用 - Code First Migrations not working 代码首次迁移 - Code first migrations 使用“代码优先迁移”为数据库播种时,哪种方法最理想? - Which method is ideal when Seeding a database with Code First Migrations? 如何首先使用EF代码重置数据库迁移? - How to reset database migrations using EF code first?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM