Model First Development With Entity Framework

In my earlier article we learned about the Code First Approach in Entity Framework. So in this article we will learn how to develop a simple .Net application with Entity Framework using the Model First Approach.

We know that there are the following 3 approaches in EF:

  1. DB First
  2. Model First
  3. Code First

So, let us create a sample application and see how to use the Model First approach for generating database driven .Net applications.

Create a Console Application named "ModelFirstDemo" then select the C# language.

console application

Right-click on your project and add a new item of "ADO.Net Entity Data Model" named "Employee".

add new item

ado dot net

Then it will ask you to select the Model Content, so we will select "Empty Model" and click Finish:

empty EF Dsigner model

After this an empty designer is displayed as shown below:

model

Since the name of this approach is Model First, the purose of this is to create the Model that will later generate the database for us.

Now right-click on the design surface and say add new entity.

add new

Name the new entity Employee as shown below:

add entity

A few points to note here are:

  1. The Entity set will automatically create the pluralize object.

    For example I have given my entity name as Employee so the entity set will be a collection of employee objects, in other words "Employees".

  2. The "Create key property" checkbox will create the primary key column named "Id" with data type Int32.

  3. We can change the Property name to "EmpID" or "EmployeeId" and so on.

So, now we have a model with only one entity "Employee" with only 1 column "Id" that is the primary key. Now let's add 2 more columns to this. Right-click on the entity and say add scalar property as shown below:

property

Add 2 scalar properties of type string named "Name" and "City". So now our Employee entity will look as in the following:

Employee entity

Note: By default when we add a scalar property the default data type is "String". If you want to change it then simply right-click on the scalar property and go to properties and then under "Type" select what is needed.

add property

Since this approach is Model First there is no database created yet, so let us see now how to generate a database.

Right-click on the designer and select "Generate database from Model" and use the following procedure:

Generate database from Model

Click on new connection and select SQL Server and provide the required credentials.

I am giving a database name that does not exist so now it will ask me whether I want to generate the new database. I will say Yes.

new database

data source

connection

Click on Yes and click Next for the following screen:

selection data connection

On the next screen you will see DDL generated with a SQL script as shown below:

DDL

Click on Finish to close this wizard and open the Employee.edmx.sql file.

Employee

Now we can execute this script that will generate the table called "Employees" in my newly created database called "ModelFirst". To execute this script click on the Execute icon as shown below:

ModelFirst

After executing the script we are ready with our database that has a table called "Employees".

Let us fire a few queries and see if we are able to connect to the database and insert a new employee.

Put this code in Program.cs and run the project.

  1. class Program  
  2. {  
  3.    static void Main(string[] args)  
  4.    {  
  5.       EmployeeContainer container = new EmployeeContainer();  
  6.       Employee emp = new Employee { Name = "Prashant", Address = "Mumbai"};  
  7.       container.Employees.Add(emp);  
  8.       container.SaveChanges();  
  9.       Console.WriteLine("New Employee Added !!!");  
  10.    }  
  11. }  
Output

Output


Verify if this has really worked and has inserted a new employee in the DB.

Open the Employees table and click on "Open Table Definition", this verifies that it is working.

Open Table Definition

I hope you liked this article.

Stay tuned for the next article. Keep Learning Cheers!