Here, I’m continuing with ASP.NET Web API. You can follow my ASP.NET Web API series from the following links –
In this series of articles, we’ll work with the entity framework, which is an Object/Relational Mapping (ORM) framework, that is enabled for us to work with the relational database. So this framework has many features such as performance, maintainability and productivity etc.
Hence, let’s start creating ASP.NET Web API with the entity framework. In this, I will be using a three tier application, so we need to add three projects in my solution- one is Service layer, the second is data layer, and the last one will be a Web API project. Here, I’m using Visual Studio 2013.
Let’s create a project for Web API. Open Visual Studio 2013, go to new and choose the project.
Hence, let’s start creating ASP.NET Web API with the entity framework. In this, I will be using a three tier application, so we need to add three projects in my solution- one is Service layer, the second is data layer, and the last one will be a Web API project. Here, I’m using Visual Studio 2013.
Let’s create a project for Web API. Open Visual Studio 2013, go to new and choose the project.

Expand Installed => Templates => Visual C# and choose an ASP.NET Web Application from the menu, give a name to your Web API project with which you want to do work and finally click “OK” button.

Select a template. I am going to create a Web API. Therefore, select Web API template from the list of the templates. Click OK button to create a Web API project, including MVC features.

Our Web API project has been created. Now, I need to add a service layer project in my solution. Go ahead and right click onto solution and a new project.

Choose ASP.NET Web Application from the menu, give it a name and finally create it by pressing OK button.

This figure is for the templates. In the Service layer project we’ll add a class and an interface. Hence, select an empty template and click the OK button to create it.

In the following screenshot, our two projects have been created. In the end, I need to add a project, which will be responsible for our data.

Create a ASP.NET Web Application for the Data layer and give it a name; whatever you want to do.

Select an empty template for our project, create a project by hitting OK button.

Hence, I have now three projects in my solution, shown below:

First of all, I want to take entity framework's latest version from the NuGet Package Manager. For this, right click on the references in the Data layer. Test the project and select manage NuGet Packages.

Here, I have the entity framework. If you don’t find it easily, you can searc for it from the top right search box. Click the install button to install it.

Hence, the latest version of entity framework is installing.

You can see in the screenshot, that we’ve a Green check mark.

We have installed the entity framework to our selected project.

The DataLayerTest project also has a reference of System.Data.Entity. Right click the references and add a reference.

Check System.Data.Entity from the menu namespaces and click OK button to add it.

All is set for the DataLayerTest project. Now, our Service layer is dependent on the data project, as you know if you have ever worked with the three tier architecture. Right click references under ServiceLayerTest project and add a reference.

Scroll the solution and select the projects. You’ll find DataLayerTest project. Check it and add it to the Service Layer by hitting OK button.

Screenshot shows I’ve added a reference of DataLayerTest in the ServiceLayerTest project.

Afterwards, I need to add an entity framework from NugGet Package Manger in the ServiceLayerTest project. Right click the references and add NuGet.

Click the Install button to install it.

Here an entity framework is installing.

Entity framework is installed successfully, as shown in following screenshot:

In the solution, as you can see, I have a project of Web API, which is dependent on the Data layer and Service layer. Add a service for this.

Check both the layers and click OK button to add it in the Web API project.

As you can see, DataLayerTest and ServiceLayerTest reference have been added successfully into our WebAPITest project.

Now, I need to create a database. Our Data layer will be responsible for an access to the database, which I’ll create. Just open Microsoft SQL Server and connect it. Here, I’m using SQL Server 2014.

Create a database by right clicking on the databases folder and select create a new database.

Give a name to your database. I have given it a name; Employee, click the OK button to create it.

In the database, we need to create a table so right click on the tables under your database and create a table.

Here, I have five columns in my table.

Set Employee ID as a primary key, right click EmpId and select set the primary key option.

All is set, now save your table.

Give a name to your table. Here, I given it Employee, as shown below:

You must fill some details in the table which we can use in our Service layer to show the data, as we expected in the end on the Browser. We can call the methods in our project with these details.

Here, I have two employees' IDs with their details.

Now, I have to add an edmx file in the Data layer to access the database which I’ve just created. Right click the Data layer test project and a new item.

In the Data panel, choose ADO.NET Entity Data Model and give a name to it.

Choose EF Designer from the database model and click Next button.

Click the new connection string to retrieve it from the database.

After clicking the connection string, you have to choose SQL remote Server name from the dropdown list and choose an appropriate name.


































Mark KeyPosted Jul 2, 2016, 1:17 PM
Very good article. Thanks for covering EF Database-first!
Rajeev PunhaniPosted Jul 1, 2016, 5:14 AM
Nice Article.
Santhakumar MunuswamyPosted Jun 25, 2016, 12:30 AM
Good work. Keep it up
Amit MishraPosted Jun 24, 2016, 12:06 AM
thanks to all
Pradeep SahooPosted Jun 23, 2016, 9:08 PM
Nice share ...very helpful for beginners
Kuppurasu NagarajPosted Jun 23, 2016, 1:31 PM
Nice Sharing..
Prasanna MuraliPosted Jun 23, 2016, 10:14 AM
Nice one....
Vignesh ManiPosted Jun 23, 2016, 8:22 AM
nice
Gowtham KPosted Jun 23, 2016, 4:22 AM
Good One, Thanks for sharing:)
Debasis SahaPosted Jun 23, 2016, 2:25 AM
Good One..
Manas MohapatraPosted Jun 23, 2016, 1:55 AM
Screenshots are more helpful for Beginners.
Bhavik PatelPosted Jun 23, 2016, 12:23 AM
good
RajaPosted Jun 23, 2016, 12:06 AM
Good one...
kalu singh raoPosted Jun 22, 2016, 1:40 PM
Nice...