Let’s start.
What is Entity Framework
Entity Framework is an ORM framework. ORM stands for Object Relational Mapping.
What is an Object Relational Mapping framework?
Object Relational Mapping framework automatically creates classes, which are based on the database tables and the opposite is also true, i.e., it can also automatically generate necessary SQL to create the database tables, which are based on the classes.
Let’s understand this with an example.
Create "Departments" and "Employees" tables.
- Create table Departments
- (
- ID int primary key identity,
- Name nvarchar(50),
- Location nvarchar(50)
- )
-
- Create table Employees
- (
- ID int primary key identity,
- FirstName nvarchar(50),
- LastName nvarchar(50),
- Gender nvarchar(50),
- Salary int,
- DepartmentId int foreign key references Departments(Id)
- )
Now, let’s insert some data.
- Insert into Departments values ('IT', 'Mumbai')
- Insert into Departments values ('HR', 'Mumbai')
- Insert into Departments values ('Payroll', 'Pune')
-
- Insert into Employees values ('Akshay', 'Phadke', 'Male', 60000, 1)
- Insert into Employees values ('Milind', 'Daware', 'Male', 45000, 3)
- Insert into Employees values ('Raghvan', 'Nadar', 'Male', 70000, 1)
-
- Insert into Employees values ('Mary', 'Ratnam', 'Female', 30000, 2)
Now, if we want to populate this data's various departments, their respective locations are required to create first two class files for the employees and Departments. Now, we need to write ADO.NET code to retrieve the data from the database once the data is retrieved from the database, which we need to create various business objects and populate the data.
With Entity Framework, we can do all this automatically, we just need to provide the database schema.
Create a new "Empty ASP.NET Web Application" and name it as Demo.
Installing Entity Framework
- Click Tools - NuGet Package Manager - Manage NuGet Packages for the solution.
- Click Browse tab in Manage NuGet Packages Window.
- Type"EntityFramework" in the search textbox on the top right hand corner.
- Finally, click Install.
Thus, Entity Framework has been installed.
Now, let’s add a ADO.NET Entity Data Model as you need to right click on the project -> Add New Item ->Data and select ADO.NET Entity Data model, as given below.
Name it as EmployeeModel and click OK. Select EF from the database option, as shown below.
Click "New Connection" and select the Server in which you had created the database. Afterwards, select second option button (Yes, include the sensitive data in the connection string.), which is to include the connection string in our web.config file.
Click Next -> select the tables and click Finish.
Now, it’s going to generate the entities model automatically, as shown below.
Notice that it has added the file EmployeeModel in our solution and it has department and Employee classes. The properties of the department are Id, name and location. These corresponds to the column to the underlying database and notice that we have Navigation property here.
Hence, a department can contain multiple employees. Thus, we have Employees property there .We are going to use this property and going to fetch all the employees, which belong to that department .
Similarly, an employee belongs to a specific department. Hence, there is a department navigation property along-with all the other properties. Basically, this will correspond to the columns in the underlying database.
Hence, a department can have many employees, so one-to-many relationship is there.
Now, let's go ahead and add a Webform to our project, add a GridView control and EntityDataSource control.
Now, lets configure the Entity Data Source, as given below.
Select named connection. Select the dropdown; the name which you had used for the connection string, which is stored in the web.config file.
Click Next
Select the entity sets as Department, as shown above and click Finish.
Hence, we have our EntityDataSource configured. Now, let’s associate the DataSource to the GridView control, as given below.
Select EnitityDataSource1.
Now, click on Edit columns and add a Template field, as given below.
Specify the name for HeaderText as Employees.
Now, click Edit Templates, as given below.
Drag and drop another GridView control. Subsequently, click on the Edit DataBindings of this GridView control, as given below.
We want to display Employees information.
Select Custom binding option. We need to use Eval method and keep in mind that there is a navigation property called Employees on the department object.
Hence, it’s going to pull the employee information and display in the second GridView.
With all these changes, let's save and run the application.
Now, look at the output. We got Id, Name and location but we didn’t get the Employee details. It is basically because we need to add one more thing here. Select the properties of Entity DataSource.
By default, it’s going to load only Department details for us.
Specify the Employees navigation property .This is going to load the employee details for us. Hence, with this change, save the details and run the application.
Here, we got the output. Now, we don’t want to display ID and department Id. Click Edit columns and add Four BoundFields, as given below.
Click OK. Set Autogenerate columns= False. Now, let's run the app and see the output.
In this demo, we have used schema first approach in an Entity Framework. We can also use Model First or Code First approaches.