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".

using System.Data.Entity; 

namespace EntityAPI.Models

{

    public class ItemsContext : DbContext

    {

        // You can add custom code to this file. Changes will not be overwritten.

        // 

        // If you want Entity Framework to drop and regenerate your database

        // automatically whenever you change your model schema, add the following

        // code to the Application_Start method in your Global.asax file.

        // Note: this will destroy and re-create your database with every model change.

        // 

        // System.Data.Entity.Database.SetInitializer(new System.Data.Entity.DropCreateDatabaseIfModelChanges<EntityAPI.Models.ItemsContext>());

       public ItemsContext() : base("name=ItemsContext")

        {

        }

        public DbSet<Novel> Novels { getset; }

        public DbSet<Item> Items { getset; }

        public DbSet<ItemDetail> ItemDetails { getset; }

 

    }

}

 

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:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Data.Entity;

namespace EntityAPI.Models

{

    public class ItemsContextInitializer : DropCreateDatabaseIfModelChanges<ItemsContext>

 

    {

        protected override void Seed(ItemsContext context)

        {

            var novels = new List<Novel>()            

            {

                new Novel() { Name = "Revolution 2020", DealPrice = 140, SellingPrice = 145 },

                new Novel() { Name = "A Calender too Crowded", DealPrice = 295, SellingPrice = 300 },

                new Novel() { Name = "How That yor are rich", DealPrice = 100, SellingPrice = 110 }

            };

            novels.ForEach(p => context.Novels.Add(p));

            context.SaveChanges();

 

            var item = new Item() { Customer = "Smith" };

            var od = new List<ItemDetail>()

            {

                new ItemDetail() { Novel = novels[0], Quantity = 2, Item = item},

                new ItemDetail() { Novel = novels[1], Quantity = 4, Item= item }

            };

            context.Items.Add(item);

            od.ForEach(o => context.ItemDetails.Add(o)); 

            context.SaveChanges();

        }

 

    }

}

Up Next
    Ebook Download
    View all
    Learn
    View all