简体   繁体   English

更改asp.net mvc模型的EF Code First模型后,如何在Azure中正确更新SQL数据库?

[英]How to properly update SQL database in Azure after EF Code First model of asp.net mvc model change?

It seems like I always have a variety of problems doing this, and usually I end up nuking the db out of frustration and rebuilding, but obviously there has to be some way to do this. 似乎我在执行此操作时总是遇到各种各样的问题,通常我最终会因为无奈而烦恼并重新构建数据库,但是显然必须有某种方法可以做到这一点。

I have an existing asp.net mvc web app living with its sql db in azure. 我有一个现有的asp.net mvc网络应用程序,其SQL数据库在azure中生活。 Works fine has some data that can be replaced but, again, the point is to learn how to update model without destroying the database. 正常工作的数据可以替换,但是,重点是学习如何在不破坏数据库的情况下更新模型。

In VS2017 I add one property public string ScreenShot { get; set; } 在VS2017中,我添加了一个属性public string ScreenShot { get; set; } public string ScreenShot { get; set; }

I make some small changes to my mvc and web api controllers to handle this extra property. 我对我的MVC和Web API控制器进行了一些小的更改,以处理此额外属性。 I update my localdb via packmanager console and add-migration addprop and update-database . 我通过packmanager控制台以及add-migration addpropupdate-database add-migration addprop Works fine, run it locally, no probs. 工作正常,在本地运行,没有问题。

Goto publish, goto settings, check update database. 转到发布,转到设置,检查更新数据库。 Click publish. 单击发布。 It hangs for like 5 or 6 min and I get: 它挂了大约5或6分钟,我得到:

Warning : A project which specifies SQL Server 2016 as the target platform may experience compatibility issues with Microsoft Azure SQL Database v12. 警告:将SQL Server 2016指定为目标平台的项目可能会遇到与Microsoft Azure SQL数据库v12的兼容性问题。 when publishing 发布时

I try publishing several times and get the same thing. 我尝试发布几次,并得到相同的结果。 Google, look around, scratch my head, try again and it seems to publish. Google,环顾四周,挠头,再试一次,它似乎已发布。 Site opens, and somehow I have lost my bootstrap theme. 网站打开,以某种方式我失去了我的引导主题。 In fact in my Content folder I now have 4 files i believe are new: bootstrap-theme. (css,css.map, min.css, min.css.map) 实际上,在我的Content文件夹中,我现在有4个文件,我相信它们是新文件: bootstrap-theme. (css,css.map, min.css, min.css.map) bootstrap-theme. (css,css.map, min.css, min.css.map) (same prefix , different suffix) as well as what I think are virgin versions of those without theme in the name, and seems to be the default mvc theme of black and white. bootstrap-theme. (css,css.map, min.css, min.css.map) (相同的前缀,不同的后缀),以及我认为是名称中没有主题的原始版本,并且似乎是默认的mvc主题黑和白。 When I goto my app and I get an generic error and checking elmah I get: 当我转到我的应用程序时,出现一般错误并检查elmah,我得到:

System.InvalidOperationException: The model backing the 'TaskTrackerContext' context has changed since the database was created. System.InvalidOperationException:自创建数据库以来,支持“ TaskTrackerContext”上下文的模型已更改。 Consider using Code First Migrations to update the database ( http://go.microsoft.com/fwlink/ ? LinkId=238269). 考虑使用代码优先迁移来更新数据库( http://go.microsoft.com/fwlink/?LinkId=238269 )。

Well I really thought thats what I did. 好吧,我真的以为那就是我所做的。 Almost forgot one thing! 几乎忘了一件事! Maybe this is where my problem lies: I actually had to run two migrations-in addition to the model change I dropped a column that had never been used (scaffolding a controller for a DTO version of one of my models added it to my context which created a table). 也许这就是我的问题所在:我实际上必须运行两次迁移-除了更改模型外,我还删除了一个从未使用过的列(为一个模型的DTO版本搭建一个控制器,将其添加到我的上下文中,创建了一个表)。

I did create a new branch before making any of these change so I could just revert back but at some point I have to make this work and have to understand how to do it without destroying my db and remaking fresh. 在进行任何更改之前,我确实创建了一个新分支,因此我可以还原,但是在某些时候,我必须完成这项工作,并且必须了解如何做到这一点,而又不破坏我的数据库并重新制作新的。 A check in SQL object explorer shows it added the ScreenShot column to my table but didn't remove the unused table. 对SQL对象资源管理器的检查显示,它已将ScreenShot列添加到了我的表中,但没有删除未使用的表。

This works for me : In visual Studio, go to publish settings and then select the option 这对我有用:在visual Studio中,转到发布设置,然后选择选项

Execute Code First Migration 执行代码优先迁移

Please see the screenshots below 请看下面的截图 发布页面

发布设置

Kind regards 亲切的问候

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

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