Entity Framework In MVC - Part One

Today I will give a brief introduction about Entity framework and how to implement Entity framework in MVC.

What is Entity framework?

Entity framework is an ORM (Object Relational Mapping) tool. 

Entity Framework

 

Object Relational Mapping (ORM) is a technique of accessing a relational database; .i.e., whatever has tables and store procedure these things we interact with database in class, method and property of the object format.  

Features of ORM in entity framework

  • Map our database types to our code types
  • Avoid repetitive data access code
  • Access code automatically based on the data model class
  • Support a clean separation of concerns and independent development that allows parallel, simultaneous development of application 
  • Easily reuse the data object
  • Application Maintainability

How to Install and Update using NuGet

Install-Package EntityFramework

Update-Package EntityFramework

Now we will see the practical from basic so first we will start using console application and how to use entity framework in console application so for this, open the visual studio and go file option and select console and click okay

Entity Framework

And click OK

After opening the console application, Entity framework is not installed by default.

Entity Framework

 

In the above pic, it has not installed Entity framework so first we will install the Entity framework.

Go to Tools option ->New Get Package Manager -> Package manager

Entity Framework

After that write the command “Install-Package EntityFramwork” in command window

Entity Framework

And press enter.

Or you can check this link for all versions

In Entity framework we follow the three approaches for communicating with database

  1. Database First
  2. Code First
  3. Model First

Now we have to add Entity so here I am going to use database first approach when we have an already-existing database then we will prefer the database first approach. So for this right click our solution explorer and go add and select data in left panel and then select ADO.Net Entity Data Model.

Entity Framework

Entity Framework

Click next button, after that new connection and enter your sql credential.

Entity Framework

After that click Ok.

And then click next and select your table.

Entity Framework

And then click finish button,

Entity Framework

After that we will write the code for retrieving the data from our sql database. In that I created an employee table and using this we will program the retrieve operation.

Now open the program class file and first add the necessary namespace.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. Now we will write program  
  5. for bind the data  
  6. class Program {  
  7.     static void Main(string[] args) {  
  8.         ExampleDBEntities objEntity = new ExampleDBEntities();  
  9.         List < EmployeeDetail > lstEmp = new List < EmployeeDetail > ();  
  10.         lstEmp = objEntity.EmployeeDetails.ToList();  
  11.         Console.WriteLine();  
  12.         Console.WriteLine("     Name" + "         " + "Address" + "    " + "EmailId" + "                 " + "Mobile Number" + "  " + "Dept Name");  
  13.         Console.WriteLine("------------------------------------------------------------------------------");  
  14.         foreach(var item in lstEmp) {  
  15.             Console.WriteLine(item.EmpName + "  | " + item.Address + " |  " + item.EmailId + "  |  " + item.MobileNo + " |  " + item.DeptName);  
  16.         }  
  17.         Console.ReadLine();  
  18.     }  
  19. }  
Entity Framework

Summary

In this article, we saw a brief introduction of Entity framework and how to install it. And also we saw how to retrieve the data from sql server using the database-first approach. In the next article we will see about the code first approach in Entity framework using MVC.


Similar Articles