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,
- public class Department
- {
- [Required]
- public int DepartmentID { get; set; }
- [Required]
- public string DepartmentName { get; set; }
-
- }
Add another class and name it as Employees and write the following code,
- public class Employees
- {
- [Required]
- public int EmployeeID { get; set; }
-
- [Required]
- public string FirstName { get; set; }
-
- public string LastName { get; set; }
-
-
- public int DepartmentID { get; set; }
- public Department Department { get; set; }
-
- }
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
- Add the Model class,
Figure 4
- 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,
- public class DepartmentContext : DbContext
- {
-
- public DepartmentContext() : base("name=DepartmentContext")
- {
-
-
- }
-
- public System.Data.Entity.DbSet<MyWebAPI.Models.Department> Departments { get; set; }
-
- public System.Data.Entity.DbSet<MyWebAPI.Models.Employees> Employees { get; set; }
- }
Seed the Database
Now, open the Package manager console and enter the following commands,
- Enable-migrations
This will add the folder called migration with code file named configuration.cs as shown in figure 8,
Figure 8
- 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,
- context.Departments.AddOrUpdate(new Department { DepartmentID = 1, DepartmentName = "Infrastructure" },
- new Department { DepartmentID = 2, DepartmentName = "HR" });
-
- context.Employees.AddOrUpdate(new Employees { EmployeeID=1,FirstName="Raja",LastName="Sekar",DepartmentID=1},
- new Employees { EmployeeID=2,FirstName="Arun",LastName="Kumar",DepartmentID=1},
- new Employees { EmployeeID=3,FirstName="Pradeep",LastName="Raj",DepartmentID=2});
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.