Using Entity Framework in Web API Part-2

Introduction

In Part 1 of this "Using Entity Framework in Web API " series of articles we explained how to create a application and add the Model class. Now in this article we will explain how to add the Web API Controller with the Entity Framework.

Step 1

Use the following procedure to create the Controller:

  • In the Solution Explorer.
  • Right-click on the Controller Folder.
  • Select "Add" -> "Controller".
  • Select Template "API Controller with read/write actions, using Entity Framework".

    Select API Controller with Entity Framework

  • Select "Model class" from the drop down list:

    Select Model Class

  • Now select "DataContext" by clicking on the new data context.

    Select Data Context

  • Now see the Solution Explorer.

    Solution Explorer display changes in Project

Step 2

Now perform some changes in the Context file "ItemsContext".

  1. using System.Data.Entity;   
  2. namespace EntityAPI.Models  
  3. {  
  4.     public class ItemsContext : DbContext  
  5.     {  
  6.         // You can add custom code to this file. Changes will not be overwritten.  
  7.         //   
  8.         // If you want Entity Framework to drop and regenerate your database  
  9.         // automatically whenever you change your model schema, add the following  
  10.         // code to the Application_Start method in your Global.asax file.  
  11.         // Note: this will destroy and re-create your database with every model change.  
  12.         //   
  13.         // System.Data.Entity.Database.SetInitializer(new System.Data.Entity.DropCreateDatabaseIfModelChanges<EntityAPI.Models.ItemsContext>());  
  14.        public ItemsContext() : base("name=ItemsContext")  
  15.         {  
  16.         }  
  17.         public DbSet<Novel> Novels { getset; }  
  18.         public DbSet<Item> Items { getset; }  
  19.         public DbSet<ItemDetail> ItemDetails { getset; }  
  20.     }  
  21. }   

Step 3

Now add a database initializer to the Model class.

  • In the "Solution Explorer".
  • Right-click on the "Model Folder".
  • Select "Class".

    Create Databse Initializer Class

  • Click on the "OK" button.

Add the following code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Data.Entity;  
  6. namespace EntityAPI.Models  
  7. {  
  8.     public class ItemsContextInitializer : DropCreateDatabaseIfModelChanges<ItemsContext>  
  9.     {  
  10.         protected override void Seed(ItemsContext context)  
  11.         {  
  12.             var novels = new List<Novel>()              
  13.             {  
  14.                 new Novel() { Name = "Revolution 2020", DealPrice = 140, SellingPrice = 145 },  
  15.                 new Novel() { Name = "A Calender too Crowded", DealPrice = 295, SellingPrice = 300 },  
  16.                 new Novel() { Name = "How That yor are rich", DealPrice = 100, SellingPrice = 110 }  
  17.             };  
  18.             novels.ForEach(p => context.Novels.Add(p));  
  19.             context.SaveChanges();  
  20.             var item = new Item() { Customer = "Smith" };  
  21.             var od = new List<ItemDetail>()  
  22.             {  
  23.                 new ItemDetail() { Novel = novels[0], Quantity = 2, Item = item},  
  24.                 new ItemDetail() { Novel = novels[1], Quantity = 4, Item= item }  
  25.             };  
  26.             context.Items.Add(item);  
  27.             od.ForEach(o => context.ItemDetails.Add(o));   
  28.             context.SaveChanges();  
  29.         }  
  30.     }  
  31. } 


Similar Articles