预期的数据库模型与实时不一致
在这个问题中,我遇到了一个问题,即我正在为已部署的应用程序编写更新以使数据库保持最新状态我们正在部署新版本。基本概要如下:
- 从当前部署的应用程序版本开始
- 添加了使用现有数据库的新功能
- 添加了新的数据库表和关系
- 添加了依赖于新数据库结构的新功能
- 测试完成,准备部署
这里的问题是当前部署的应用程序已使用几个月并且有大量数据需要保留,因此简单地用新的替换旧的是不可行的(至少对于数据库来说不可行,但当然它适用于代码)。因此,我使用以下步骤在 SQL 中编写一个脚本,以便应用程序的更新版本在第一次启动时运行,以便对数据库进行必要的更改,而无需触及现有数据(除了填充新表):
- 使用 VS2010 的“从模型生成数据库”功能创建 .sql(该模型最初是使用“从数据库生成模型”功能创建的)
- 删除 .sql 中作用于现有表的所有部分,除了在新旧表之间添加 FK 的部分
- 使用生成的脚本构建新数据库
听起来很干净并且完成了,对吧?错误的。对于新表来说,从模型到数据库的映射完全错误。长话短说,生成模型的数据库具有以复数形式命名的表(并且映射正确且应用程序运行),并且模型生成的数据库以复数形式创建了表(与数据库所在的表的名称相同)生成了模型,但模型没有映射到它们)。解决方案最终是更改脚本以用单数命名表,然后一切都完美运行。
这里发生了什么?代码保持不变,没有对模型进行任何更改,并且旧表在整个过程中继续正常工作,但在过程中的某个地方
- 生成脚本
- 删除“新”表和约束(部署版本中尚不存在的表和约束)
- 运行脚本以重新添加表
决定为单名表的表(用户而不是用户,地址而不是地址, ETC)。
谁能向我解释一下这是如何/为什么会发生这种情况?
In this question, I was facing an issue where I was writing an update for a deployed application to bring the database up to date with the newer version we are deploying. Basic outline as follows:
- Began with currently deployed version of application
- Added new functionality that used existing database
- Added new database tables and relationships
- Added new functionality that depended on the new databse structure
- Testing complete, ready for deployment
The issue here is that the currently deployed application has been in use for a few months and has a lot of data that would need to be preserved, so simply replacing the old with the new was not viable (at least not for the database, but of course it works for the code). So I used the following steps to write a script in SQL for the updated version of the application to run the first time it starts up to make the necessary changes to the database without touching existing data (aside from populating the new tables):
- Use VS2010's "Generate database from model" functionality to create a .sql (the model was originally created using the "Generate model from database" functionality)
- Remove all parts of the .sql that act on the existing tables, except for those that add FKs between new and old tables
- Use the resulting script to build the new database
Sounds pretty clean and done, right? Wrong. The mapping from the model to the database was all wrong for the new tables. Long story short, the database that generated the model had tables named in the plural (and the mapping was correct and the application worked), and the database generated by the model created tables in the plural (identical names to what the tables where the DB generated the model, but the model did not map to them). The solution ended up being to change the script to name the tables in the singular, and then everything worked flawlessly.
What happened here? The code remained untouched, no changes were made to the model, and the old tables continued to work fine the entire time, yet somewhere in the process of
- Generate script
- Delete "new" tables and constraints (those that don't yet exist in the deployed version)
- Run script to re-add the tables
the mapping decided to be to singularly named tables (User instead of Users, Address instead of Addresses, etc).
Can anyone explain to me how/why this would happen this way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能想查看 redgate 提供的一些工具 - 用于比较两个数据库结构并生成要更新的脚本的好工具。
http://www.red-gate.com/?utm_source=google&utm_medium=cpc&utm_content=brand_aware&utm_campaign=redgate&gclid=CIamkumgw6sCFcYPfAodnGVjsQ
You might want to look at some of the tools that redgate supply - good tools for comparing two DB structures and generating a script to update.
http://www.red-gate.com/?utm_source=google&utm_medium=cpc&utm_content=brand_aware&utm_campaign=redgate&gclid=CIamkumgw6sCFcYPfAodnGVjsQ