VS 2010 数据库项目在列名称更改时删除数据
我正在测试 Visual Studio 2010 的新数据库项目功能,并且想要更改表中列的名称。我更改了创建脚本中的名称并将其部署到数据库中。生成的脚本只是删除了该列并添加了具有正确名称的新列,但所有数据都丢失了。
有没有设置不会删除列数据?
我正在寻找这个问题的“DataDude”解决方案。 (如果有的话)
PRINT N'Altering [dbo].[Users]...';
GO
ALTER TABLE [dbo].[Users] DROP COLUMN [TestX];
GO
ALTER TABLE [dbo].[Users]
ADD [Testn] NVARCHAR (50) NULL;
GO
谢谢 基思
I am testing out the new database project features of Visual Studio 2010 and want to change the name of a column in a table. I changed the name in the create script and deployed it against the database. The script that was generated just dropped the column and added a new column with the correct name, but all the data was lost.
Is there a setting that will not drop the column data?
I am looking for the "DataDude" solution to this issue. (If there is one)
PRINT N'Altering [dbo].[Users]...';
GO
ALTER TABLE [dbo].[Users] DROP COLUMN [TestX];
GO
ALTER TABLE [dbo].[Users]
ADD [Testn] NVARCHAR (50) NULL;
GO
Thank you,
Keith
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通过单击“视图”->“使用架构视图”数据库架构视图
展开表及其列。
右键单击该列,然后单击 Refactor ->重命名...
在“新名称”字段中更改名称,并选中“预览更改”框。
请注意,它不仅更改列名称,还更改可能引用该列的存储过程。
在数据库项目内部,会创建一个显示名称更改的重构日志文件。
当针对现有数据库部署新模式时,DataDude 似乎会查看重构日志文件和 dbo._Refactorlog 表,以确定需要针对数据库处理哪些重构。
以下是使用此过程生成的代码,该过程更改了存储过程中也引用的列名称:
Keith
Use the Schema View by clicking View -> Database Schema View
Expand the tables and its columns.
Right click the column and click Refactor -> Rename...
Change the name in the New Name field with Preview Changes box checked.
Notice it changes not only the column name, but also the stored procedures that may be referencing that column.
Inside the Database project a refactorlog file is created that shows the name change.
When the new schema is deployed against the existing database, it appears that DataDude looks at the refactorlog file and the dbo._Refactorlog table to determine which refactors need to be processed against the database.
Here is the code it generated using this procedure the change a column name that was also referenced in a stored procedure:
Keith
如果表很小,您可以创建一个包含正确列的新表,然后将旧表插入新表,并随时更改列。
如果表很大,并且您无力承担复制整个表的时间、RAM 或精力,您可以
更改表添加 new_column
然后更新表集 new_column=old_column
然后更改表删除列 old_column
。语法当然被简化了。
If the table is small, you can create a new table with the correct columns, and then insert from the old table into the new one, changing columns as you go.
If the table is large, and you cannot afford either the time, RAM, or effort to copy the entire table, you can
alter table add new_column
thenupdate table set new_column=old_column
thenalter table drop column old_column
.Syntax is of course simplified.
Visual Studio 数据库项目处理架构更改的方式容易造成这种混乱。 VS 很难判断您是简单地重命名一列(并且应该保留数据)还是删除该列并添加另一列。我的公司在数据库模式源代码控制产品上工作了很长一段时间,该产品也存在所有这些相同的问题。
最后,我开始相信处理架构更改的最佳方法是 K.斯科特·艾伦(杰夫·阿特伍德引用)。本系列文章应该让您走上更好的数据库版本控制解决方案的道路。
The way that Visual Studio Database Projects handle schema changes lends itself to this kind of confusion. It's very difficult for VS to tell if you simply renamed a column (and should preserve the data) or removed the column and added a different one. My company worked for quite a while on a database schema source control product that had all of these same issues.
In the end, I've come to believe that the best way to handle schema changes is that proposed by K. Scott Allen (referenced by Jeff Atwood). This series of articles should set you on a course towards a better database version control solution.