Introduction To Code First Approach In Entity Framework

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.

  1. public class Department  
  2.     {  
  3.   
  4.         public int Id { get; set; }  
  5.         public string Name { get; set; }  
  6.         public string Location { get; set; }  
  7.         public List<Employee> Employees { get; set; }  
  8.     }   

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.

  1. public class Employee  
  2.     {  
  3.         public int Id { get; set; }  
  4.         public string FirstName { get; set; }  
  5.         public string LastName { get; set; }  
  6.         public string Gender { get; set; }  
  7.         public int Salary { get; set; }  
  8.         public Department Department { get; set; }  
  9.     }  

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.

  1. public class EmployeeDBContext : DbContext  
  2.     {  
  3.   
  4.         public DbSet<Department> Departments { get; set; }  
  5.         public DbSet<Employee> Employees { get; set; }  
  6.     }  

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.

  1. public class EmployeeRepository  
  2.     {  
  3.   
  4.         public List<Department> GetDepartments()  
  5.         {  
  6.             EmployeeDBContext employeeDBContext = new EmployeeDBContext();  
  7.             return employeeDBContext.Departments.Include("Employees").ToList();  
  8.         }  
  9.     }  

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.

  1. Insert into Departments values ('IT''Mumbai')    
  2. Insert into Departments values ('HR''Mumbai')    
  3. Insert into Departments values ('Payroll''Pune')    
  4. Insert into Employees values ('Akshay''Phadke''Male', 60000, 1)    
  5. Insert into Employees values ('Milind''Daware''Male', 45000, 3)    
  6. Insert into Employees values ('Raghvan''Nadar''Male', 70000, 1)    
  7. 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!!


Similar Articles