Important: This kind of project can only be used with Premium or Ultimate version. It does not exist in Professional or Express edition.
Why We Need this Database Project?
Already we know how to create a database project in Visual Studio and how to manage how to publish, generate script and deploy).
In this article, we walkthrough how to use database project for existing database and how to update database without using Code First Migrations in EF 6.0.
Creating the Visual Studio Project
First, create a MVC project using EF 6.0 Code First approach .
Open your Visual Studio and go to "New Project -> Web -> Visual Studio 2012 -> ASP.NET MVC4 Web Application".
MyDatabaseProjectWithEF6.0 and Click “OK”
The solution would be like below:
Add the “Domain classes” like below:
Right click on Models -> Add -> NewClass
I have added four classes -
Now it’s time to create a context.
Add new class to the Model folder name it as “
StudentContext” by inheriting the “
If we run the project, it will create the database with the Name “
StudentContext” as follows:
Now if we change anything in our domain classes, we need to do “Code First Migrations” http://msdn.microsoft.com/en-in/data/jj591621.aspx.
In the initial stage of the project according to requirement, we may add or remove Domain Classes (columns/Table). In this case, we have no need to do migrations every time.
But we can add database project from existing database and we can make modifications in this project we will publish.
Creating the Database Project
Let us walkthrough how to add database project, how to make changes and how to publish the changes.
Right click on Solution -> Add -> New Project -> SQL Server -> SQL Server Database Project
Rename it as
StudentDatabase and click “OK”.
Now, we have database project and we need to import the existing database as follows.
Right click on
StudentDatabase -> Import -> Database.
It will give Import Database wizard.
Click on “new Connection”, it will open Connection Properties wizard.
Add server name, Authentication (Windows or SQL) and select the database from “Select or enter a database name” dropdown.
Then click “Start” and then “Finish”.
Now we have imported our database successfully as shown below:
As of now, we haven’t changed and published anything. Now we are going to change
Student Domain Class.
I am adding “
Age” property in
Now the model/class has been changed. In this case, we need code first migrations, but we will update the database through “database project”.
StudentContext -> dbo -> Tables -> Students.sql will have one Table and T-SQL Pane.
Now, we are going to update the database.
Before updating the database, we need to save the profile (saving
connectionString and all related data).
Right click on
StudentContext -> Publish it will open publish window.
Click on “Edit” button. It will open the “Connection Properties window”.
Click “Ok”, it will establish the connection between database and our application.
We are publishing database for the first time, so we need to create a profile.
It will store in local folder and it creates an XML file as shown below:
Now onwards we have no need to click on publish “just double click on StudentDatabase.Publish.xml” fully loaded window.
Now click on “Publish” button. It will create the script and it will publish change to database.
It has given me an error.
Here I am using SQLServer 2008 so I need to change this to 2008 as follows:
Right click on database project -> Properties -> Project Settings ->Target FlatForm -> Select SqlServer 2008 and save changes.
Again publish by double clicking on .xml file.
It has been published successfully, now you can see the change in database.
Finally, we have seen how to add database project, how to establish the connection to existing database and how to publish the change to database.
- July 24th, 2014: First version