Click here to Skip to main content
Click here to Skip to main content

Entity Framework Code First using Database Project

, 25 Jul 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
This article explains how to create database project to existing solution explorer and how to import the existing database that is already created by using Entity Framework code first approach. It also explains how to update the database.

Introduction

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".

Rename as 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 - Student, Address, Parent and Courses.

Student Class

Address Class

Parent Class

Course Class

Now it’s time to create a context.

Add new class to the Model folder name it as “StudentContext” by inheriting the “DbContext”.

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.

Click “OK”.

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 Student Class.

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.

History

  • July 24th, 2014: First version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Naveen Kumar Matteddula
Software Developer (Junior) Technossus Software Services
India India
No Biography provided
Follow on   Twitter   Google+

Comments and Discussions

 
GeneralMy vote of 5 PinprofessionalMihai MOGA13-Aug-14 3:20 
GeneralRe: My vote of 5 PinmemberNaveen Kumar Matteddula17-Aug-14 18:27 
GeneralGood Work!! PinmemberSunny Setia29-Jul-14 2:53 
QuestionThank you PinmemberGaurav Madaan28-Jul-14 18:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.141015.1 | Last Updated 25 Jul 2014
Article Copyright 2014 by Naveen Kumar Matteddula
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid