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.
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
And click OK
After opening the console application, Entity framework is not installed by default.
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
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
- Database First
- Code First
- 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.
Click next button, after that new connection and enter your sql credential.
After that click Ok.
And then click next and select your table.
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.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- Now we will write program
- for bind the data
- class Program {
- static void Main(string[] args) {
- ExampleDBEntities objEntity = new ExampleDBEntities();
- List < EmployeeDetail > lstEmp = new List < EmployeeDetail > ();
- lstEmp = objEntity.EmployeeDetails.ToList();
- Console.WriteLine();
- Console.WriteLine(" Name" + " " + "Address" + " " + "EmailId" + " " + "Mobile Number" + " " + "Dept Name");
- Console.WriteLine("------------------------------------------------------------------------------");
- foreach(var item in lstEmp) {
- Console.WriteLine(item.EmpName + " | " + item.Address + " | " + item.EmailId + " | " + item.MobileNo + " | " + item.DeptName);
- }
- Console.ReadLine();
- }
- }
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.