Walkthrough: Partition a Database Project by Using Composite Projects
You can use composite projects to better manage a large database by partitioning it into a set of dependent database projects that are deployed together. This walkthrough illustrates the following tasks:
Create a database project in which your database schema is defined.
Create another database project in which your tables for a particular schema are defined.
Create a third database project in which the stored procedures for a particular schema are defined.
Configure, build, and deploy the third project, together with its dependencies.
Prerequisites
You must have installed Visual Studio Team System Database Edition and have access to a server that is running SQL Server.
Create a Database Project for Your Schema
To create the source database project
On the File menu, point to New, and click Project.
The New Project dialog box opens.
In the Project types list, expand the Database Projects node, and then click SQL Server 2005.
Note
If your target database has a different version of SQL Server, you can click the type of project that matches your version.
In the Templates list, click SQL Server 2005 Database Project.
In Name, type MySchemaProject.
In Location, type or browse to the path in which you want to create the database project.
In Solution name, type MyCompositeSolution, and click OK.
The database project is created and appears in Solution Explorer. Next you define a schema in the project.
To define a schema
On the View menu, click Database Schema View.
Expand the MySchemaProject node, expand the Schemas node, right-click the Schemas node, point to Add, and click Schema.
The Add New Item dialog box opens.
In the Templates list, click Schema.
In Name, type Person, and click Add.
On the File menu, click Save All.
Next, you build the project.
To build the schema project
In Solution Explorer, right-click the MySchemaProject node, and click Build.
Note
You must build the project so that references to the schema that is defined in project can be resolved in the procedures that follow.
Next, you create a project for the tables in the Person schema.
Create a Database Project for Your Tables
To create the source database project
On the File menu, point to Add, and click New Project.
The New Project dialog box opens.
In the Project types list, expand the Database Projects node, and then click SQL Server 2005.
Note
If your target database has a different version of SQL Server, you can click the type of project that matches your version of SQL Server.
In the Templates list, click SQL Server 2005 Database Project.
In Name, type MyTablesProject.
In Location, type or browse to the path in which you want to create the database project, and click OK.
The database project is created and appears in Solution Explorer.
Next, you add a reference to the schema project before you can define tables in the table project.
To add a reference to the schema project
In Solution Explorer, expand the MyTablesProject node, right-click the References node, and click Add Database Reference.
The Add Database Reference dialog box opens.
In Database Reference, verify that Database projects in the current solution is specified.
Note
If this solution did not contain the projects to which you referred, you would instead click Database project schema (.dbschema). You might use this approach if, for example, you wanted to prevent team members who were developing stored procedures from unintentionally changing the schema or table projects.
Click MySchemaProject.
Important Note: Do not specify server variables and values or database variables and values when you define references in a composite project. Because no variables are defined, the referenced project is assumed to share the target server and database of the current project.
Click OK.
Next, you create two tables in the new schema.
To define tables in the Person schema
On the View menu, click Database Schema View.
In Schema View, expand the MyTablesProject node, expand the Schemas node, right-click the Person node, point to Add, and click Table.
Note
To keep this walkthrough brief, you do not update the table definitions.
The Add New Item dialog box opens.
In the Templates list, click Table.
In Name, type Contacts.
Right-click the Person node, point to Add, and click Table.
The Add New Item dialog box opens.
In the Templates list, click Table.
In Name, type StateProvince.
Next, you build the database project.
To build the tables project
In Solution Explorer, right-click the MyTablesProject node, and click Build.
Next, you create a third database project for your stored procedures.
Create a Database Project for Your Stored Procedures
To create the database project for stored procedures
On the File menu, point to Add, and click New Project.
The New Project dialog box opens.
In the Project types list, expand the Database Projects node, and then click SQL Server 2005.
Note
If your target database has a different version of SQL Server, you can click the type of project that matches your version.
In the Templates list, click SQL Server 2005 Database Project.
In Name, type MySprocProject.
In Location, type or browse to the path in which you want to create the database project, and click OK.
The database project is created and appears in Solution Explorer.
Next, you modify the stored procedures project so that it refers to the schema project.
To add a reference to the schema project
In Solution Explorer, expand the MySprocProject node, right-click the References node, and click Add Database Reference.
The Add Database Reference dialog box opens.
In Database Reference, verify that Database projects in the current solution is specified.
Click MySchemaProject.
Important Note: Do not specify server variables and values or database variables and values when you define references in a composite project. Because no variables are defined, all projects to which you refer are deployed to the same target server and database.
Click OK.
Next, you modify the stored procedures project so that it refers to the MyTablesProject project.
To add a reference to the tables project
In Solution Explorer, right-click the References node under the MySprocProject node, and click Add Database Reference.
The Add Database Reference dialog box opens.
In Database Reference, verify that Database projects in the current solution is specified.
Click MyTablesProject.
Important Note: Do not specify server variables and values or database variables and values when you define references in a composite project. Because no variables are defined, all projects to which you refer are deployed to the same target server and database.
Click OK.
Next, you create two tables in the schema.
To define stored procedures that refer to the tables in the Person schema
On the View menu, click Database Schema View.
In Schema View, expand the MySprocProject node, expand the Schemas node, right-click the Person node, point to Add, and click Stored Procedure.
The Add New Item dialog box opens.
In Name, type uspCountContacts, and click Add.
The stored procedure is added to the project and appears in the Transact-SQL (T-SQL) editor.
In the T-SQL editor, update the procedure to match the following code:
CREATE PROCEDURE [Person].[uspCountContacts] AS SELECT COUNT(*) FROM [Person].[Contacts]
On the File menu, click Save All.
Next, you configure, build, and deploy the MySprocProject and its dependencies.
Configure, Build, and Deploy Your Database
To configure MySchemaProject for deployment
In Solution Explorer, right-click the MySchemaProject node, and click Properties.
On the properties page, click the Deploy tab.
In the Deploy action list, click Create a deployment script (.sql) and deploy to database.
Click Edit next to Target Database Settings.
Specify a connection to the target server where you want to deploy this database project, and then click OK.
In Target database name, type MySprocProject.
Important Note: You must specify the same target database name for all three projects that make up the composite project or deployment will fail.
On the File menu, click Save All.
Next, you configure properties for MyTablesProject.
To configure MyTablesProject for deployment
In Solution Explorer, right-click the MyTablesProject node, and click Properties.
On the properties page, click the Deploy tab.
In the Deploy action list, click Create a deployment script (.sql) and deploy to database.
Click Edit next to Target Database Settings.
Specify a connection to the target server where you want to deploy this database project, and then click OK.
In Target database name, type MySprocProject.
Important Note: You must specify the same target database name for all three projects that make up the composite project or deployment will fail.
On the File menu, click Save All.
Next, you configure properties for MySprocProject.
To configure MySprocProject for deployment
In Solution Explorer, right-click the MySprocProject node, and click Properties.
On the properties page, click the Deploy tab.
In the Deploy action list, click Create a deployment script (.sql) and deploy to database.
Click Edit next to Target Database Settings.
Specify a connection to the target server where you want to deploy this database project, and then click OK.
In Target database name, type MySprocProject.
Important Note: You must specify the same target database name for all three projects that make up the composite project or deployment will fail.
On the File menu, click Save All.
Next, you build the database project.
To build and deploy the development database
In Solution Explorer, right-click the MySprocProject node, and click Build.
In Solution Explorer, right-click the MySprocProject node, and click Deploy.
Your database is built and deployed to the specified target server, together with its dependencies.
Next Steps
You can use this method to work on smaller sections of a database at the same time. You can also refer to the .dbschema files instead of the database projects if you want the developers who work on the stored procedures to be isolated from the table and schema definitions.