Sunday, May 12, 2013

Entity Framework Step By Step 2 – Model First

We’ll see how we can user Model First Development workflow of Entity framework with a very simple example in step by step so you can easily understand the concept.


  • 1. You will need to have Visual Studio 2010 or Visual Studio 2012 installed to complete this walkthrough.
  • 2. If you are using Visual Studio 2010, you will also need to have NuGet installed.

Step 1:  Create Application

  • 1. Create New Console Application.
  • 2. Name it as “ModelFirstSample”

Step 2:  Add Entity Model item to project

  • 1. Right click on the project & select add new item.
  • 2. In the new item selection menu select “Data” then select “ Entity Model”.
  • 3. Name the item as “BloggingModel


  • 4. Then in the Entity Data Model Wizard select “"Entity Model” click finish.


Step 3:  Create Model

  • Right-click on the design surface and select Properties
  • In the Properties window change the Entity Container Name to BloggingContext
  • Right-click on the design surface and select Add New -> Entity…
  • Enter Blog as the entity name and BlogId as the key name and click OKimage

Step 4:  Add Properties

  • Right click on the entity & select Add New –> Scalar Property –> Add Name


Step 5:  Set Properties of the scalar property added to the entity created

  • Right click on the scalar property you want to edit, them select properties.
  • You can then set the properties listed below as per your requirements in the domain model.


Step 6:  Create the “Post” entity

  • Create a entity named “Post” following the same steps with the following properties.

    1.  PostId

2. Title

Step 7:  Create a Relation

  • Right click on the Design surface & select Add New->Association
  • Select the properties of the association as per the requirement.
  • In this example Blog will have many Posts , so the association will have the properties as below.
  • Make Sure “Add foreign key property to the Posts Entity “is checked.


Now we have the simple model created as below in the


Step 8:  Generate the Database

  • Right click on the design surface and select “Generate Database from Model”


  • In “Generate Database Wizard” select new connection
  • Set the connection properties.


  • Select OK and you will be asked if you want to create a new database, select Yes
  • Select Next and the Entity Framework Designer will calculate a script to create the database schema
  • Once the script is displayed, click Finish and the script will be added to your project and opened
  • Right-click on the script and select Execute, you will be prompted to specify the database to connect to, specify (localdb)\v11.0 or.\SQLEXPRESS, depending on which version of Visual Studio you are using

Now connect to your database server using “SQL Management Studio” There you can see the new database is created as per the model we specified in the designer.


So we are done. In the Next post I will explain how to read & write data & how to handle the model changes.

No comments :

Post a Comment