This blog post assumes you are using Visual Studio 2010. However, most of the information is relevant to Visual Studio Team System 2008 Database Edition GDR.
We get a lot of questions on the forum for which the answer is to use partial projects and/or database project references. Before you choose one or the other method to implement your database design it is helpful to understand the pros and cons of each feature. This blog post attempts to give you some insight into these pros and cons.
Before we discuss Partial Projects and Database Project References it is important to understand how the project system behaves. Specifically, it is important to understand how it represents your design internally. The following figure describes how a database or server project named “Foo” is represented. In other words, the figure describes what Data Dude consumes to create the in-memory model of your database design. We refer to the in-memory model as the “Schema Model.” The Schema Model represents four things
The reason it is important to understand this is because it profoundly affects what happens when you deploy your project. If an object or setting is not in the Schema Model it will not be compared to the target database and added, modified or deleted when you deploy the project.
Partial projects allow you to share source code files between projects without creating multiple copies of the actual source code. In other words, the same physical file is incorporated directly into the Schema Model for two (or more) projects. See the following figure. In this diagram some files have been exported from project “Foo” and imported into project “Bar.” It’s important to understand that the same physical .sql file(s) is being consumed by each project. The Schema Model for “Bar” does not understand that the source files are being maintained in another project (“Bar”).
This brings us to the Pros and Cons of partial projects
This is obviously a big pain and is the number one reason people don’t like partial projects. The work around is to define all the objects that you plan to export from a particular project in one .sql file.
Database project references allow you to reference T-SQL objects defined in another database or server project. The objects defined in the referenced project are included as [External] objects in the Schema Model of the referencing project. See the following figure.
As the figure indicates, the objects defined in the referenced project are not truly included in the Schema Model for “Foo.” If an object is referenced that is not defined in the Schema Model the validation component tries to find the object’s definition in the [External] set of objects.
When you create your database project reference you have to decide if you will be referencing the objects from the referenced project using two part, three part or four part names. You control this behavior by defining Database Reference Variables, as explained in the section below titled Configuring the Database Reference.
Two part names
The Data Dude product team refers to project references that use two part names as “Composite Projects.” The reason being you are implementing a single database that is “composed” of two or more projects. The term “Composite Project” is a euphemism for a project reference that is configured for two part name references.
There are two common ways that developers organize their code
It is important to understand that each project must be deployed independently and they must be deployed in the correct order. If you use Visual Studio to deploy your project or solution it will automatically deploy everything in the correct order. If you use a command line tool like msbuild or vsdbcmd you have to make sure you do the deployment in the proper order.
Three part names
By defining a database name using a literal value or SQLCMD variable you are configuring the database reference such that references to SQL objects must be defined using three part names. See the configuration information below for details.
If you need to refer to objects that are defined in SQL Server’s master database this is the configuration you will use. You will define a Database Reference to the appropriate master.dbschema file that we ship and you will set the database variable name to master after enabling the Literal checkbox.
Four part names
By defining a server variable you are configuring the database reference such that references to SQL objects must be defined using four part names.
There are several configuration settings available when defining a database project reference. It’s helpful to understand what each setting actually affects. I’ve included a screenshot of the dialog below.
Database Reference
This is the setting that allows you to point to what you are actually referencing. You have two choices
When you build a database or server project the artifact created is a .dbschema file. It’s important to understand that both choices actually end up pointing to a .dbschema file. Choosing the first option simply looks at the current build output folder setting and automatically figures out the path and filename. In other words, you don’t have to browse for the file. This also means that if you don’t actually build the project you are referencing the database reference is effectively broken. You’ll see a warning icon in Solution Explorer.
Database Reference Variables
Use these settings when you need to use three or four part names. In other words, you are defining a database reference to a project that will be deployed to a different physical database.
Updating Schema Objects and Scripts
By selecting this option Data Dude will do a search and replace of any server or database names throughout your code with the variables you defined.
Resolution of External Objects
The option Suppress errors caused by unresolved references in the referenced project is the most relevant to topic of this blog post. This option affects how the validation component behaves. Imagine you are defining a reference to a project which in turn references another project (we’ll call it “Third”). Think about the previous explanation of how the Schema Model is populated. When the validation component is validating the [External] objects it will not be able to find objects that are defined in the project “Third.” If you leave this option off you will get errors for the objects defined in “Third.” By setting this option you cause the validation component to not attempt to resolve objects that are referenced from the project being referenced.
Now let’s look at the pros and cons of Database Project References.
Hopefully this is helpful. Let me know if you still have questions on this subject that I haven’t addressed.