ASP.NET Web API With Entity Framework 6 Code First Technique - Part 1

ASP.NET Web API is a framework for building the HTTP services to reach a browser, mobile device and many other clients, it is an ideal platform for building RESTful application.

In this article we will see how to create a RESTful application using ASP.NET WEB API with entity framework.

Create an empty WEB API application using an installed web template in visual studio as in the following figures:

                                                                      Figure 1
                                                                         Figure 2 

Let me start from creating model classes.

In the solution explorer, right click the model folder, select ADD, then Class and name it as Department, write the following code,
  1. public class Department  
  2. {  
  3.     [Required]  
  4.     public int DepartmentID { getset; }  
  5.     [Required]  
  6.     public string DepartmentName { getset; }  
  8. }  

Add another class and name it as Employees and write the following code,

  1. public class Employees  
  2. {  
  3.      [Required]  
  4.      public int EmployeeID { getset; }  
  6.      [Required]  
  7.      public string FirstName { getset; }  
  9.      public string LastName { getset; }  
  12.      public int DepartmentID { getset; } //Foreign Key  
  13.      public Department Department { getset; } //Navigation Property  
  15. }  

Entity Framework will create database table based on the model classes.

DepartmentID - defines the foreign key into the Department table.

The employee class contains the navigation property to the related department table.

Add Web API Controllers

Note: Before adding the controller build your application once.

Let us create Web API controllers that support CRUD operations.

In Solution Explorer, right-click the Controllers folder. Select Add, then Controller.

                                                                                        Figure 3       
  1. Add the Model class,

                                                                   Figure 4

  2. Click on + icon button and add the Department context class,

                                                                         Figure 5 

                                                                            Figure 6  
Follow the same step and create one more controller called Employees Controller as in the following figure 7,
                                                              Figure 7 
The code which is generated in newly created DepartmentContext class,
  1. public class DepartmentContext : DbContext  
  2. {  
  4.     public DepartmentContext() : base("name=DepartmentContext")  
  5.     {  
  8. }  
  10.     public System.Data.Entity.DbSet<MyWebAPI.Models.Department> Departments { getset; }  
  12.     public System.Data.Entity.DbSet<MyWebAPI.Models.Employees> Employees { getset; }  
  13. }  
Seed the Database

Now, open the Package manager console and enter the following commands,         

  1. Enable-migrations

    This will add the folder called migration with code file named configuration.cs as shown in figure 8,

                                           Figure 8

  2. Add-Migration Initial

    This will create a database,

                                                                                                       Figure 9

    Check your Web config file and ensure the SQL connection string,

    Open configuration.cs file under migration folder and add the following code in seed method,
    1. context.Departments.AddOrUpdate(new Department { DepartmentID = 1, DepartmentName = "Infrastructure" },    
    2.     new Department { DepartmentID = 2, DepartmentName = "HR" });    
    4.         context.Employees.AddOrUpdate(new Employees { EmployeeID=1,FirstName="Raja",LastName="Sekar",DepartmentID=1},    
    5.            new Employees { EmployeeID=2,FirstName="Arun",LastName="Kumar",DepartmentID=1},    
    6.            new Employees { EmployeeID=3,FirstName="Pradeep",LastName="Raj",DepartmentID=2});   
  3. Update-Database

    This will run the seed method,

                                                                                           Figure 10

Check you database which is created,

                                       Figure 11

This is my Department table,

                        Figure 12 

This is my Employee table,

                           Figure 13 

Now our API's are ready: 

Department API's,

GET: api/Departments – Get the Department records.

                                                   Figure 14 

GET: api/Departments/{department ID} – Get the Department Records based on ID.

                                         Figure 15 

POST: api/Departments – add the new record in department using this API,

                                                                     Figure 16  

PUT: api/Departments/5 – update the department data based on id,

                                                                     Figure 17 
 The Update result as shown in figure 18,
                                                   Figure 18  

DELETE: api/Departments/5—delete the record in department table using this API.

                                                   Figure 19 

This article is going long and long, there are more things need to be discussed regarding the topic, so in part- 2 let me share something about handling the related entities and routing in ASP.NET WEB API. 

I hope you have enjoyed this article.  Your valuable feedback, question, or comments about this article are always welcomed. 

Similar Articles