VS 2010 Database Project: Building and Deployment

Posted Sun, May 2 2010 22:52 by Deborah Kurata

The new Visual Studio 2010 Database Project, as described in this prior post, makes it easy to manage the scripts for your database from within Visual Studio. This includes building the schema scripts from the individual database object scripts and deploying the scripts back to the database.

One of the ways that the new VS 2010 Database project is significantly different from the Database project available in prior versions of Visual Studio is the build and deployment process.

The old VS 2003/2005/2008 Database project was just a container for your scripts. You could right-click on the database in Server Explorer to generate the script to the project. When you made changes to the script, you could right-click on the script and select to apply the script to the database. But you had to manually perform these tasks.

And with the old style Database projects, it was easy to forget to apply your script changes to the database. It also was not easy to keep everything in synchronization. For example, if you later made a table change, it is easy to forget to adjust the related stored procedures as well.

Instead of being a simple repository, the new 2010 Database project is alive. The scripts in your Database project define your database just like the files in your code projects define your application. And the build and deployment process can apply the changes to your actual database.

This post details how to use the VS 2010 Database project and deploy your scripts back to your database.

Start by adding a VS 2010 Database project to your solution following the steps in this prior post. Then write the scripts into your project from an existing database as detailed in this prior post. Or write your own scripts in the project.

After you've made any changes to those scripts, you will want to deploy them back to the database.

To build the Database project, right-click on the Database project and select Build. OR you can use the Build menu.

When you build the Database project, Visual Studio validates your scripts and writes the database schema to a file with a .dbschema extension.

image_thumb7

To deploy the scripts to your database, right-click on the Database project and select Deploy. Or you can use the Build menu.

When you deploy the Database project, Visual Studio builds a deployment script.

image_thumb9

You can then run that one script to apply all of the information from your database object scripts to the development database.

But if you want to ensure that your development database is always up to date with the scripts in your Database project, you can set your Database project properties so that any changes are automatically deployed to the development database when you run your application (or build the solution).

Double-click on the Properties node of the Database project in Solution Explorer to open the Database project properties. Select the Deploy tab:

image

Set the Deploy action to "Create a deployment script (.sql) and deploy to the database". Then use the Edit button to the right of the Target connection (cut off in the above screen shot) to set the connection to your development database. Once you set these project settings, they will remain set unless you change them.

Each time you deploy the Database project, using the menu options or by just running the application within Visual Studio, Visual Studio will immediately apply all script changes directly to the database.

image

In this example, I added a table script for an Invoice table. When I ran the application, Visual Studio built and deployed the Database project scripts, creating the Invoice table in the database.

Use these features of the Database project any time you want to keep the master copy of your database schema defined in your scripts and ensure that the schema is in sync every time you run your application within Visual Studio.

Enjoy!

Filed under: , , , , ,

Comments

# VS 2010 Database Project: Adding Stored Procedures

Monday, May 03, 2010 1:37 AM by Deborah's Developer MindScape

The new Visual Studio 2010 Database Project, as described in this prior post , makes it easy to work

# VS 2010 Database Project: Adding Stored Procedures

Monday, May 03, 2010 1:41 AM by Deborah's Developer MindScape

The new Visual Studio 2010 Database Project, as described in this prior post , makes it easy to work

# re: VS 2010 Database Project: Building and Deployment

Monday, May 03, 2010 9:37 AM by D. Lambert

Deborah -

Has this functionality changed since the VS2008 version?  I'm using VS2008 DB projects to generate deployment scripts, and I end up hand-editing them to deal with some of the assertions it drops in the top of the script.  

Specifically, in my shop, db scripts have to go through a DBA staff for promotion through environments, and the QA and higher environments are locked down enough that I can't deploy directly to those DB's.  I end up having to deploy against a schema-equivalent DB that I maintain, and then strip out the server-checking code so this script can be promoted to other environments.

It would be immensely helpful if I had more control over the generation of this script so that I could make it more environment-neutral.

Any hope of this in VS2010?

# re: VS 2010 Database Project: Building and Deployment

Monday, May 03, 2010 9:47 AM by Deborah Kurata

Hi D. Lambert -

If you are using the the Database project under the "Other Projects" node in VS 2008, then this Database project is its replacement. And yes, it is new.

But if you are referring to the Microsoft SQL Server project under the Database Projects node, I have never used that and don't know if/how it was changed for VS 2010.

Hope this helps.

# re: VS 2010 Database Project: Building and Deployment

Monday, May 03, 2010 5:14 PM by James Sheldon

Deborah - These are very helpful!

Do you know if you can drop tables using these types of projects. Wasn't able to see any way to do that.

Thanks,

James

# re: VS 2010 Database Project: Building and Deployment

Tuesday, May 04, 2010 12:54 AM by Deborah Kurata

Hi James -

Do you have a Data menu with a Schema compare option? It can compare your existing database to another database and it builds in appropirate Drop commands.

Hope this helps.

# re: VS 2010 Database Project: Building and Deployment

Wednesday, May 05, 2010 11:38 AM by Tom H.

Hi Deborah,

Thanks for the informative post!

Does this method handle DML type scripts? For example, if we need to add a day to a datetime column in one of the tables based on some kind of criteria.

Also, can it handle upgrades that jump versions? Or do I need to always be pointing to a server that is on the version that my code started at when I began my work?

Thanks!

# 使用VS2010的Database 项目模板统一管理数据库对象

Saturday, May 08, 2010 10:44 AM by ASP.NET Chinese Blogs

阅读: 53 评论: 1 作者: geff zhang 发表于 2010-05-08 23:19 原文链接 Visual Studio 2010 有一个数据库项目模板:Visual Studio Database

# re: VS 2010 Database Project: Building and Deployment

Monday, May 17, 2010 3:06 PM by Glen

The new database project structure, though sounding good, is the worst thing to happen to our development process, and is significantly holding back our adoption of VS2010!

# re: VS 2010 Database Project: Building and Deployment

Monday, May 17, 2010 7:36 PM by Deborah Kurata

Hi Glen -

Can you provide more detail on specifically the issues you have?

# re: VS 2010 Database Project: Building and Deployment

Thursday, May 20, 2010 2:17 PM by Joe Bloggs

Thanks for the information but what I don't understand is how to post updates that are made to one script (eg a stored procedure) to the DB.  The impression I get is that you have to post all changes to the DB via the Deploy process. But if you have multiple people working on the DB or you are working on multiple files and only want to post changes made to one file can you do this like you could in VS2008?

# re: VS 2010 Database Project: Building and Deployment

Monday, May 24, 2010 10:29 AM by Deborah Kurata

Hi Joe -

You can right-click and select Execute SQL. However, unless this is a new stored procedure, you will get the message "There is alredy an object named 'xxx' in the database.

So you need to change "Create" to "Alter" at the top of your stored procedure before you execute it and then change it back after you execute it.

It is not a very nice process, but it works.

Hope this helps.

# re: VS 2010 Database Project: Building and Deployment

Thursday, May 27, 2010 2:39 AM by En

Why was the create proc template changed, removing the following:

"IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'mysproc')

BEGIN

DROP  Procedure  mysproc

END

GO

This made deploying so much easier!

# re: VS 2010 Database Project: Building and Deployment

Thursday, May 27, 2010 10:31 AM by Deborah Kurata

If you use the new built-in deployment features, the generated scripts handle this.

So this is only a pain point when executing an individual script.

# re: VS 2010 Database Project: Building and Deployment

Thursday, May 27, 2010 4:27 PM by Noam Ben-Ami

Joe - You can update only some parts of the database by using the "Schema Compare" feature in the Data menu. Just select the objects you want to deploy (or rather, unselect the ones you don't want deployed.)

HTH.

# re: VS 2010 Database Project: Building and Deployment

Thursday, June 10, 2010 9:53 AM by Robert Friberg

@James

There is a "Generate DROP statements for objects that are in the target database but that are not in the database project" option. You can find it under properties -> Database.sqldeployment in VS2010 solution explorer.

# re: VS 2010 Database Project: Building and Deployment

Tuesday, June 29, 2010 2:11 PM by Buddy Lindsey

This is a great resource and good information. However, you only keep talking about development database.

Is it possible to do all the work on the dev database then when you are done can you point it at the production DB and it will update database structure of the Production DB to what the Dev DB structure is?

Thanks.

# VS 2010 Database projects - treat your database as code

Thursday, July 29, 2010 11:11 PM by Geek #

VS 2010 Database projects - treat your database as code

# VS 2010 Database projects - treat your database as code

Friday, July 30, 2010 4:36 PM by Geek #

VS 2010 Database projects - treat your database as code

# re: VS 2010 Database Project: Building and Deployment

Tuesday, August 17, 2010 7:06 PM by Rashmi

Hello !

Very useful infromation ! I have a quick question... lets say I setup a datatabase project and after that someone added some stored procedure to the database with SQL server management studio. Even after I deploy the project, it does not pickup these newly added stored procs. Is there any way to do this synchronization?

From you post, I found out how to deploy stored procs to database...I am trying to find reverse for it.

Thank you very much,

-Rashmi

# re: VS 2010 Database Project: Building and Deployment

Tuesday, August 17, 2010 10:44 PM by Deborah Kurata

Hi Rashmi -

There is no *easy* way to do this. I have found that I need to keep a copy of the database under a different name, call it Database2. Then when someone updates Database1 I can run the Data | Schema Compare to compare Database1 to Database2. It sees that I have some new stored procedures and will create the new scripts into my project.

Hope this helps.

# re: VS 2010 Database Project: Building and Deployment

Tuesday, August 31, 2010 11:50 AM by dbdw

Rashmi,

You could compare the database schema against the project schema to create scripts for the SPROCs added through management studio.

# re: VS 2010 Database Project: Building and Deployment

Tuesday, August 31, 2010 11:56 AM by dbdw

Hi Deborah,

I like everything about the new database projects, except when depolying version 2 and upwards!

When a table is altered the deployment scripts creates a temporary tabe and then copies the data. And then drops the original table and rename the temp table. This is not going to work when deployed against the Production database ! Example: If I have 50 Million rows the deployment is going to take hours together. Is there a work around for deployment to create ALTER TABLE scripts instead?

Thanks in advance...

# re: VS 2010 Database Project: Building and Deployment

Wednesday, November 03, 2010 2:10 PM by Rita

Hello,

I'm having some problems compiling a database project because some SP have temporary tables. For instance a SP with:

...

SELECT TOP 5000 IDENTITY(INT,1,1) AS N  

INTO #Tally

FROM Master.dbo.SysColumns sc1, Master.dbo.SysColumns sc2

...

INSERT INTO @Parameters (ParameterValue)

SELECT * FROM #Tally

...

returns:

Error 198 DAC05000: Invalid object name '#Tally'.

for "INSERT INTO @Parameters (ParameterValue)

SELECT * FROM #Tally "  (running the SP directly in the database it works fine)

Is it possible to use temporary tables in SP (in database projects) ? How ?

Thanks in advance.

# re: VS 2010 Database Project: Building and Deployment

Thursday, November 11, 2010 4:17 PM by Glen

It seems to me that the problem with the VS2010 database projects is that they "treat your database as code".   The problem is that databases are not code.  They are data stores with some code attached.

The idea of developing "disconnected" doesn't make any sense to me at all.  I'm not writing this stuff with the hope that it will magically work once it's all put together.  I write and test and write and test with very small steps in between.

While I appreciate the basic syntax checking that VS2010 provides I can get feedback just as quickly in management studio by making a change and pressing F5.  

Bottom line: Me no like.

# re: VS 2010 Database Project: Building and Deployment

Tuesday, November 16, 2010 8:04 PM by Deborah Kurata

Glen -

I think you may misunderstand. You don't run disconnected. You have a database. The "code" is the set of scripts. So you generate the scripts for your tables, constraints, keys, stored procedures, and so on. THAT is what comprises your database project. That way you can use TFS or another source code control on your database scripts.

But when you run, you are running against your actual SQL Server database.

You can set properties in the project to define that it should deploy any script changes to the database each time you run. Or you can leave that property off and handle that manually.

Hope this clarifies things.

# re: VS 2010 Database Project: Building and Deployment

Tuesday, March 01, 2011 6:37 AM by Gilles

Hi,

I have a problem while deploying procedures on a custom database.

In project properties i have in Deploy property Tab Target Database Name = MyCustomDatabase.

The procedures are always created on master database.

I checked in all project files a reference to master database.

Can you tell me what is problem?

# re: VS 2010 Database Project: Building and Deployment

Saturday, March 12, 2011 4:56 AM by Tushar Agarwal

I have created a tool, which would allow you to run the scripts like you did in Visual Studio 2008.

# re: VS 2010 Database Project: Building and Deployment

Tuesday, May 24, 2011 8:48 AM by Brian Barnett

Another way to get the 'IF EXISTS ... DROP' back is to modify the Visual Studio item templates.  They are located in C:\Program Files\Microsoft Visual Studio 10.0\VSTSDB\Extensions\SqlServer\Items.  Of Program Files (x86) if you are on a 64-bit OS.

For example the stored procedure template is called Create Procedure 2008.sql.  Simply add the 'IF EXISTS ... DROP' to the template.

# re: VS 2010 Database Project: Building and Deployment

Thursday, June 09, 2011 1:42 PM by Srini

Good information.

If i have to deploy to the database whenever i trigger the new build on the project.

Is there way to do that?

# re: VS 2010 Database Project: Building and Deployment

Friday, June 17, 2011 4:05 AM by Bruce

Hello,

I am trying to build a database project with VS2010 premium but I get a nasty :

C:\Program Files\MSBuild\Microsoft\VisualStudio\v10.0\TeamData\Microsoft.Data.Schema.TSqlTasks.targets(56,5): Error MSB4036: The "SqlBuildTask" task was not found

Have you encountered this problem, or do you have any suggestions as to how I could resolve this error.

Thanks in advance

# re: VS 2010 Database Project: Building and Deployment

Tuesday, June 21, 2011 9:41 AM by Deborah Kurata

Hi Bruce -

Some suggestions were posted here:

devlicio.us/.../issues-compiling-vs2010-solutions-with-web-projects-from-nant.aspx

Check the comments area.

Hope this helps.

# re: VS 2010 Database Project: Building and Deployment

Tuesday, August 02, 2011 1:15 PM by Manoj

Hi, I'm using the VS 2010 Database project to deploy my database to a hosting site. It deploys great with the default scripts it generates. But I want to make a change where everytime b4 it deploys i want to drop all the tables, Sp's, function etc. and recreate the tables using the default script. i have the drop schema objects script but when i use it as a pre-deploy..the deploy function only drops the tables & moves on to the post deploy script..it does create any tables like it was doing with the default scripts that was generated. I hoe i did not confuse you

# re: VS 2010 Database Project: Building and Deployment

Monday, September 26, 2011 7:13 AM by Ananth

Hi,

Is there a possibility to find the changes done on the database projects after a certain date.

I mean can we get the deployment scripts based on date range.

For ex: My previous release happened on lets say July 1 2011.

So for the next release, i want the deployment script to be generated based on changes done after July 1 2011.

This reqmt helps to get reviewed the changes from DBA, otherwise i have to give the full db deployment script to be reviewed. its painful..

thanks in Advance... Regards

# re: VS 2010 Database Project: Building and Deployment

Monday, September 26, 2011 7:14 AM by Ananth

Hi,

Is there a possibility to find the changes done on the database projects after a certain date.

I mean can we get the deployment scripts based on date range.

For ex: My previous release happened on lets say July 1 2011.

So for the next release, i want the deployment script to be generated based on changes done after July 1 2011.

This reqmt helps to get reviewed the changes from DBA, otherwise i have to give the full db deployment script to be reviewed. its painful..

thanks in Advance... Regards

# re: VS 2010 Database Project: Building and Deployment

Thursday, December 15, 2011 11:08 AM by CT

i believe the real strength is that these projects can be deployed to TFS and tracked in source control.

i believe that is a very important feature to track the changes and what changes were made for what purpose.

from a corporate perspective, being able to create deployments scripts is an enormous feature if a production team is deploying the projects over SIT, QA, Prod phases.

maybe someone has already mentioned it, if you are creating the db  for the first time, you can first create the db in sql server studio to a point where you are satisfied, with all procs, permissions etc, and get VS to import all the scripts for you from sql server.

this project mostly seems to track the changes in a corporate environment and i find it amazingly helpful to create delta scripts, which contain only the changes i made.

# re: VS 2010 Database Project: Building and Deployment

Thursday, February 02, 2012 6:48 AM by Build Error in DB project VS 2010

I have upgrade the VS2008 to VS2010, i am getting the below error in the DB projects.

The database is not available in the local manchine, it's presented in seperate server.

I don't need to deploy the project, but need to get build successfully.

Done executing task "SqlBuildTask" -- FAILED.

Done building target "DspBuild" in project "MyOperation.dbproj" -- FAILED.

Done executing task "CallTarget" -- FAILED.

Done building target "DBBuild" in project "MyOperation.dbproj" -- FAILED.

Done building project "MyOperation.dbproj" -- FAILED.

Help me if any one know!!

# re: VS 2010 Database Project: Building and Deployment

Friday, February 24, 2012 9:36 AM by Bill

All was working fine until I introduced a script with 49000 insert statements for static data. we use a post deploy script via SQLcmd. I receive an outofmemoryexception. You might argue that this amount of data should be run in from outside the project, but why should it.

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above: