In this article, we will see the Code First approach in Entity framework. Again, this is the continuation of my previous article "Entity Framework Model First Approach". Refer to these links,
Let’s start
Code-first approach allows us to create our custom classes first, and based on those custom classes, Entity Framework can generate a database automatically for us. Let's understand this with an example. We will be modifying the example we worked with in previous articles.
From our previous solution, we already have EmployeeModel.edmx file.
We will delete that file because we no longer require it. Also, delete EmployeeModel.edmx.sql file which contains the required SQL.
In our project, let's add a class file and name it Department.cs. So, this file is going to contain the department definition for this class.
Now, let’s add ID, name, and location properties. The department will return the list of employees, so we will be adding that too in our department class file.
- public class Department
- {
-
- public int Id { get; set; }
- public string Name { get; set; }
- public string Location { get; set; }
- public List<Employee> Employees { get; set; }
- }
Now, we will face a compilation error because Employee class doesn’t exist in our solution. So, now, let’s create the Employee class.
Add the properties in Employee class, such as ID, FirstName, LastName, gender, salary, and the department to which the employee belongs.
- public class Employee
- {
- public int Id { get; set; }
- public string FirstName { get; set; }
- public string LastName { get; set; }
- public string Gender { get; set; }
- public int Salary { get; set; }
- public Department Department { get; set; }
- }
Here, the department properties in Employee class and employees properties in Department class are navigation properties. The rest of them are scalar properties.
Now, let’s add another class file and name it EmployeeContext.cs. This EmployeeDBcontext must inherit from DBcontext which is present in System.Data.Entity. Here, in this class file, we will be assigning the departments and Employee objects.
- public class EmployeeDBContext : DbContext
- {
-
- public DbSet<Department> Departments { get; set; }
- public DbSet<Employee> Employees { get; set; }
- }
So, this DBContext class is going to interact with the database and retrieve the data for us and populate the custom objects.
Add another class file in the project and name it as EmployeeRespository.cs. So basically, this class file is going to contain our methods which are going to return the data that we want.
- public class EmployeeRepository
- {
-
- public List<Department> GetDepartments()
- {
- EmployeeDBContext employeeDBContext = new EmployeeDBContext();
- return employeeDBContext.Departments.Include("Employees").ToList();
- }
- }
So, this GetDepartments method is going to return the list of departments that exist in our table. And to retrieve the data, we have EmployeeDbcontext class which is going to interact with our database.
Now, let’s add Objectsource control to our webform and configure the datasource.
Click Next
You have multiple options there to select, insert, update, and delete the data from the datasource. But, we will be using Select here. Select the dropdown which has a list of Departments. Now, let’s associate the datasource with the GridView control.
Delete the existing database which we already created because in the code first approach, you don’t need to create a database as it will be created automatically for us.
Now, let’s run the application.
Notice that we don’t have any data here. That’s basically because the application has just created the database and the required tables.
But if you see the table, there is no data. So now, let’s manually insert dummy 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, refresh the webform.
So, this was all about the Code First approach in Entity Framework. Hope this article was helpful!!