Creating Web API Using Entity Framework With LINQ Query In MVC 4

In this blog I am showing how to create Web API using Entity Framework with LINQ Query in .NET MVC 4.

Step 1 - First we have to create Employee Table
 
 
Step 2 - Now we have to create MVC 4 Web Application
 
 
 
Step 3 - In that we have to choose Web API and click OK Button
 
 
 
Step 4 - Then you will get empty Project
Step 5 - In Solution Explorer within Controller folder you will see by default Controllers. And we have to delete both controllers.
 
 
Step 6 - After that we have to add Entity Framework (ADO.NET) in Models Folder. Right click on Models Folder select ADO.NET Entity data model.
 
 
 
Step 7 - Connect your database where you created employee table.
 
 
 
Step 8 - After creating Model you will see model diagram like this.
 
 
Step 9 - After we have to create Controller. Right click on Controller folder choose
Add->Controller and click Add button.
 
 
Step 10 - After clicking add Button you will get empty controller like this.
 
 
Step 11 - EmpController.cs Code Behind
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Net.Http;  
  6. using System.Web.Http;  
  7. using WebAPI.Models;  
  8. namespace WebAPI.Controllers  
  9. {  
  10. public class EmpController : ApiController  
  11. {  
  12. guruapiEntities db = new guruapiEntities();  
  13. // GET api/emp  
  14. public IEnumerable<employee> Get()  
  15. {  
  16. return db.employees.ToList<employee>();  
  17. }  
  18. // GET api/emp/name  
  19. public IEnumerable<employee> Get(string id)  
  20. {  
  21. var list = from g in db.employees where g.name == id select g;  
  22. return list;  
  23. }  
  24. // POST api/emp  
  25. public HttpResponseMessage Post(employee value)  
  26. {  
  27. try  
  28. {  
  29. if (ModelState.IsValid)  
  30. {  
  31. db.employees.Add(value);  
  32. db.SaveChanges();  
  33. return Request.CreateResponse(HttpStatusCode.OK);  
  34. }  
  35. else  
  36. {  
  37. return Request.CreateResponse(HttpStatusCode.InternalServerError, "Invalid Model");  
  38. }  
  39. }  
  40. catch (Exception ex)  
  41. {  
  42. return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);  
  43. }  
  44. }  
  45. // PUT api/emp/5  
  46. public employee Put(int id, string name, string mobile, string email, string address,string pwd)  
  47. {  
  48. var obj = db.employees.Where(n => n.id == id).SingleOrDefault();  
  49. if (obj != null)  
  50. {  
  51. obj.name = name;  
  52. obj.mobile = mobile;  
  53. obj.email = email;  
  54. obj.address = address;  
  55. obj.password = pwd;  
  56. db.SaveChanges();  
  57. }  
  58. return obj;  
  59. }  
  60. // DELETE api/emp/5  
  61. public void Delete(int id)  
  62. {  
  63. var obj = db.employees.Find(id);  
  64. db.employees.Remove(obj);  
  65. db.SaveChanges();  
  66. }  
  67. }  
  68. }  
Step 12 - Run the application. Then you will get browser like this. And write API link in URL box here.
 
 
 
Step 13 - Then you will get output in XML format
 
 
 
Step 14 - If you want output in JSON format then add below code in WebApiConfig.cs.
  1. var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");  
  2.   
  3. config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);  
Step 15 - JSON Format output is
 
 
Step 16 - If you want particular data then make it back slash after emp and write a name then you will get particular data. In this API we done in LINQ Query.