Here is my second article about the database project. This time, we’ll talk about the case when the project is already started and where database scripts are managed manually.
Imagine, you arrive at a project and somebody explains to you how to use the script file. You have to be careful about where you paste your script in the big file. You must check that all the tables you need are created before your part of the script is executed…
Seeing this, you talk about the database project that allows to manage all of these things alone, but somebody tells you: “Oh! I don’t know that and it’s too late, we already started the project, it would be too long to refactor all the script.”
You can answer: “It’s not too late, the database project will generate all the scripts automatically, synchronizing from the database”. Here is how to do it!
First, we open Visual Studio and we create a new database project as described in the previous article. We have our empty project created and we will know how to update it with the existing object in the database.
Go to the menu Data -> Schema Compare -> New schema comparison. A new window opens where we can select the source database and the target VS project to update:
On the left side, Source Schema, select Database and configure the connection string. On the right side, Target Schema, select Project and find the project in the list.
If you want, you can tell to the project not to synchronize some type of objects of the database. For that, go to Options:
When we have to configure all the options we want, we can launch the comparison. We obtain something like that:
We can see here that tables will be created and going down in the Columns directory, we see that columns will also be created. In the second table, we can see that a foreign key will be created.
We can also see that the synchronization option can be modified. If we don’t want to create the foreign key, we can select Create and change the value to Skip.
As we are satisfied, we are going to update the project. In the toolbar, we can see the “Write Updates” button. Let’s click it and the project is updated.
After a confirm message, you should see that the operation went successfully. Let’s check! Go to the project and, YEAH! We can see scripts for our two tables and our foreign key. It works!
Now, we know how to create a database project for an existing database. In the next article, we will see how to update this project when the database is modified directly.