Web API(CRUD operations) Using ASP.NET CORE,Entity Framework CORE and Deploy the Module in IIS.

From this you are going to learn how to configure Entity Framework core and POSTMAN. We will use reverse engineering to create an EF Model based on an existing database.

Create a database (UserDB) and a Table (UserTB)

figure 1: Table creation

Now lets create the application using VS 2017 IDE

Select New ASP.NET Core Web Application(.NET Core) and choose “Web API” project template.

Figure 2: Create a new project
Figure 3: Web API Project template

When creating a new project make sure that Authentication is set to No Authentication.

We need to add the reference to the Entity Framework core as follows by installing the package for the database.

We can do this in two ways.

  • Open package manager console and install following commands.
Install-Package Microsoft.EntityFrameworkCore.SqlServer
Install-Package Microsoft.EntityFrameworkCore.Tools
Install-Package Microsoft.VisualStudio.Web.CodeGeneration.Design
  • Right click on the project and select “Manage NuGet package” and install the packages.
  • Install this following package
Figure 4: Install Microsoft.EntityFrameworkCore.SqlServer package
Figure 5: Install Microsoft.VisualStudio.Web.CodeGeneration.Design package(Scaffolding tools to create controllers)
Figure 6: Install Microsoft.EntityFrameworkCore.Tools package( EF to create model from the database)

Lets create the connection with database and our project

  • Tools -> Connect to Database
  • It will pop up a window like this.
Figure 7: Database Connection
  • Select your server name and make the Authentication as “SQL Server Authentication”
  • Select your “UserDB” database.

Lets Reverse Engineer your model. Now it’s time to create the EF model based on the existing database.

Run this following command in the package Manager Console.

Scaffold-DbContext “Server=your server name;Database=UserDB;Trusted_Connection=True;” Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
Figure 8: Created Model classes.

It automatically creates UserDBContext based on the schema of the existing database.

Figure 9: UserDBContext.cs
Figure 10: UserTb.cs

Remove the inline context configuration

lets delete the OnConfiguring(…) method inside the UserDBContext.cs class.

Then add the following constructor which allows the configurations to be passed in the dependency injection.

Figure 11: Dependency Injection

Now we are going to Register and configure your context in Startup.cs.

In order to do this we have to make our UserDBContext as a service.

Add the following code to the Startup.cs.

Figure 12: Startup.cs

Create a Controller

We are going to do this using scaffolding process.

  • Right-click on the ControllersAdd →Controller.
  • Select Full Dependencies.
  • You can ignore the instructions in the ScaffoldingReadMe.txt file that opens

Now that scaffolding is enabled, we can scaffold a controller for the Blog entity.

  • Right-click on the Controllers folder in Solution Explorer and select Add -> Controller…
  • Select API Controller with actions, using Entity Framework and click Ok
Figure 13:API Controller with actions, using Entity Framework

then you will get a pop up window to set Model class and Data Context class

Set Model class to UserTb and Data context class to UserDBContext.

Figure 14: Add API Controller with actions, using EF
Figure 15: Controller
Figure 16: UserTbsController.cs

You can take the source code here GitHub

Deploy the Module in IIS

In order to host the ASP.NET 2.0 Web API , we have to download the Hosting Bundle here.

Figure 17: Hosting Bundle for ASP.NET CORE 2.0

Now go to your project in VS 2017 .

Right click on the project and select “Publish”. It will pop up a window as follow and click on “Create new profile”

Figure 18: Click on “Create new profile”

Then choose IIS,FTP,etc

Figure 19: Choose IIS,FTP,etc

Then choose the Publish method as “File System” and place your target location.

Figure 20: Target location and Publish method.

After you publish your code, we can check whether this is working or not when we are hosting by using the following command in command prompt.

dotnet C:\inetpub\wwwroot\testapi\UserDetailSystem.dll

The result is will be like this

Figure 21: The result

These are the following steps you have to do with IIS.

  1. Open IIS
  2. Stop default web site if you are hoping to host this API using a different port.
  3. Create a new web site.(Here i have name it as “testapi”).
  4. Create an application pool by placing the .NET CLR Version as “No managed code”.
Figure 22: Newly created Application pool.

5. Binding for the website should be as follows.

Figure 23: Bindings

The most important thing is don’t forget to make the Identity as “LocalSystem” in your application pool.

Figure 24: Make the Identity as LocalSystem.

6.Start and Browse your site/ You can send the request in Postman also.

Figure 25: Postman Result.
Figure 26: Result

Create the Remote Access the web API in another machine

When you are using a different port for the web site , there are some changes you have to follow in the Windows Firewall.

  1. Click on Advanced Settings.
  2. Click on Inbound Rules and create a New Rule.
Figure 26: Create a new inbound rule.

3.Select port

Figure 27: Select port and place your port number as Specific local path

4. Create a name for the rule as follow.

Figure 28: Rule name.

The rule you have created will be shown as this.

Figure 29: Created Inbound Rule.

Now you can browse it using http://yourIP:86/api/UserTbs/1 to access the API in both browser and postman.


Now we have created our CRUD application using Entity Framework core and Deploy our ASP.NET CORE 2.0 API in IIS.