Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS 2010 Database Project dropping data on column name change

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

like image 503
Keith Sirmons Avatar asked Jan 21 '23 22:01

Keith Sirmons


1 Answers

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:

EXECUTE sp_rename @objname = N'[dbo].[Users].[TestF]', @newname = N'TestG', @objtype = N'COLUMN';

GO
PRINT N'Altering [dbo].[ListUsers]...';

GO
ALTER PROCEDURE [dbo].[ListUsers]

AS
    SELECT [ID], [FirstName], [LastName], [TestG]
    FROM Users
RETURN 0
GO

Keith

like image 91
Keith Sirmons Avatar answered Mar 07 '23 12:03

Keith Sirmons