Visual Studio 2010: Database projects

Visual Studio 2008 introduced us database projects. These projects are also present in Visual Studio 2010. I noticed some differences already when building my photo gallery application. Let’s see how these projects work under Visual Studio 2010.

Why database projects?

Database projects are good for more than one reason. Main reason is simple – all database objects are defined in same solution where is code that is using this database. Because database project is project like the others then it is possible to add it to code repository – you get versioning this way.

You can build and deploy database projects so you get SQL-scripts and up to date database on your database server. And… I’ve heard that database projects are also able to incremental deployments.

Structure of database projects

Mygallery2010: Database project In my last posting about my photo gallery system, Packaging my gallery code, I wrote about how to separate files from web project to other projects. One of there projects was database project called MyGallery2010.Database.

You can see my database project structure on right. Currently I have definitions for tables, primary keys and foreign keys. I think it is enough for now. When project gets some more complex requirements on database then I will add new features.

One difference I noticed is that empty database project is created without deep folder structure in Visual Studio 2010. Visual Studio 2008 database projects created also folders for tables, keys and so on. Visual Studio 2010 let’s you decide how do you want to organize the definitions of your database projects.

The internal structure of database projects is very rich. Take a look at properties section – there are settings for SQL command variables, deployments, permissions and other settings. You can also add database references to Visual Studio database projects.

And one thing more – take a look at Post-Deployment and Pre-Deployment folders. You can add there special SQL-scripts that are run before and after database deployment.

Adding database objects to project

After creating new database project it is easy to start adding definitions for database objects. You can right-click on database project and select database object type from menu as show on following screenshot fragment.

Visual Studio 2010: Database menu

You can also use "Add New Item” menu selection to add new database object definition from database objects menu as shown on following screenshot.

Visual Studio 2010: Add new database object

After adding new object definition Visual Studio 2010 creates file for it and adds example definition to it. You can use it to build up your own object definition. Building your database using Visual Studio database projects guarantees pretty well that you pay attention to every object you define. I think it may decrease also bugs in database definitions.

Building and deploying database

Building and deploying database projects is not different from other projects. What is different is output.

MyGallery2010: Build results of database project

You can see that before any deployments database scripts are validated and also object dependencies are analyzed. It means that before deployment the order of changes is already clear and deployment scripts have no errors.


kick it on DotNetKicks.com pimp it Progg it 顶 Shout it

3 Comments

  • so cool ~~ thanks share

  • It is of my concern!? I cannot longer when I find the code you have replied that this will be of little dangerous help. Yes or sure I may have not this code much but Can you of course help? It error of database...... of course. Thank you!

  • Since i want to source control my database with Code, I am getting started with Database project in VS 2010.

    But when i create a new table or any object in sql it opens in Text editor and i need to write sql query.

    I am using SQL Server 2008. It's not R2 though.

    Is there anything that i can do to get the Editor while creating the tables and applying relationships in my database tables so that I dont need to write the script.

    Any help would be appreciated :)

Comments have been disabled for this content.