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)
Now lets create the application using VS 2017 IDE
Select New ASP.NET Core Web Application(.NET Core) and choose “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.
- Right click on the project and select “Manage NuGet package” and install the packages.
- Install this following package
Lets create the connection with database and our project
- Tools -> Connect to Database
- It will pop up a window like this.
- 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
It automatically creates UserDBContext based on the schema of the existing database.
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.
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.
Create a Controller
We are going to do this using scaffolding process.
- Right-click on the Controllers →Add →Controller.
- Select Full Dependencies.
- You can ignore the instructions in the
ScaffoldingReadMe.txtfile that opens
Now that scaffolding is enabled, we can scaffold a controller for the
- Right-click on the Controllers folder in Solution Explorer and select Add -> Controller…
- Select API Controller with actions, using Entity Framework and click Ok
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.
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.
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”
Then choose IIS,FTP,etc
Then choose the Publish method as “File System” and place your target location.
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.
The result is will be like this
These are the following steps you have to do with IIS.
- Open IIS
- Stop default web site if you are hoping to host this API using a different port.
- Create a new web site.(Here i have name it as “testapi”).
- Create an application pool by placing the .NET CLR Version as “No managed code”.
5. Binding for the website should be as follows.
The most important thing is don’t forget to make the Identity as “LocalSystem” in your application pool.
6.Start and Browse your site/ You can send the request in Postman also.
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.
- Click on Advanced Settings.
- Click on Inbound Rules and create a New Rule.
4. Create a name for the rule as follow.
The rule you have created will be shown as this.
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.