Please enable Javascript for better experience...
Web API Tutorial C# [Part 6] - Using Entity Framework with Web API [Database First Approach]
By Rahul Kumar Jha | Jan 10, 2020 | In Tutorials | Update: Jan 21, 2020 | Total Views [ 9976 ]
Taged In
(0 Like)
Rate

This is part 6 of Web API tutorial series. In this article, I have demonstrated using Entity Framework with Web API to communicate with SQL Server. In this article, database first approach has been explained.

Introduction

To start with Entity Framework, you need to add EntityFramework project in your solution. In this article we demonstrated each step to setup Entity Framework. We will use existing project, add EF project, import database table and replace all code with LINQ query. Finally we will validate the implemetation using MVC application.

Setting up Entity Framework Project

Step 1) To Setup EF project, let's add a class library and name it "EntityFrameworkLayer".

01

Step 2) Now try to add Entity Template. Unfortunately you will not find it as there is no such template exist as it has to install via Nuget package.

02

Step 3) Right click solution and click on "Manage Nuget Package".

03

Step 4) Search EntityFramework in nuget. It will show Entity Framework package. Now select all the projects where you want to install EF. In our case it is EntityFrameworkLayer and WebAPI project. EntityFramework 6 will get install when you click Install button.

04

Step 5) Now addd new item in EntityFrameworkLayer and Select Data tab. Select ADO.Net Entity Data Model and name it "EFEntityModel".

05

Step 6) A wizard opens, follow as mentioned below. In this demo, we are using Database-First apporach. Wizard will setup database connection, save it to App.config file, import all tables which want to import and show a diagram for the same. We imported only employee table as this is the only table we have. An entity context class will be created, here we named it PRACCONEntities and a model named PRACCONModel is created which have all POCO classes.

06

07

08

09

10

11

Here you can see the EDMX file created having tables in diagram form.

12

Here you can see the connection string added in App.config file. copy this connection string in Web API project as well.

13

Here is the entity structure.

14

Step 7) Now add a class EmployeeManager and add below code to fetch employee data from database using LINQ query.

15

Above code wll not run, add below code to it. You need to convert your data into Enumerable else it will not convert into long using Convert.ToInt64().

20

16

Step 8) In EmployeeController in Web API project, add below code to call EntityFramework methods using EmployeeManagement class.

17

Step 9) Test the Web API using MVC application.

18

19

Hope this helps you.

<< Web API Tutorial C# [Part 5] - Fetching data from SQL Server into Web API

Share this

About the Author

Rahul Kumar Jha
Rahul Kumar Jha
Founder, Developer dotnet-concept.com

Public profile: user/profile/99900001


Has working experience in different phases of Software Development Life Cycle (SDLC) in CMS, Gaming, Health Care and Financial Services domain using Agile pattern. Working experience in Design patterns, ASP.NET, MVC, ANGULAR, ANGULAR JS, Windows application, WCF, ADO.NET, SQL Server and Test Driven Development (TDD) environment with JQuery, JavaScript, N-Unit, Entity Frameworks, LINQ, Code Refactoring and Business Objects Models.

User's Comments


 
Please SignUp/Login to comment...

Or comment as anonymous...
* Name
* Email ID
Comment
 
 
 
 
 
 
Sponsors