[英]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
,然后在代码中调用迁移,如上所示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.