In this tutorial, we will learn how to create web api in asp net core using Entity Framework Core. ASP.NET Core is a lightweight runtime library. It is a cross-platform that can run on Windows, Linux, and Mac.
Note:- We will use Visual Studio code for this project. We will not create a new project in this tutorial, if you don’t know how to create a new project, then click here.
So, First-of-all, we will add model in our project, then we will add entity framework, and then see how to create web api using dot net core and entity framework core.
So, after this tutorial, you will be able to understand:
- How to create model in dot net core and angular project.
- How to add entity framework in dot net core and angular project.
- How to create web api using dot net core and angular project.
How to create web API using dot net core and angular?
Let’s start to understand “how to create web API” with the latest technology like Angular 6 and Asp Net Core 2.1.
Step # 1 => Create a new model
First-of-all, go to project folder structure, then make a new folder with the name “Models”. Now right click on newly created folder and add a new file with the name of “Student.cs”. Now copy the below code and thand paste into the Student model class.
Note:- Don’t forget to add libraries above the code.
In the above code, we have created properties for student’s record with some validations like Required and String Length.
Step # 2 => Adding Entity Framework Core.
Now, in this step, we will add an entity framework in our project. To do this, we need to run some commands to add an entity framework in our project.
Note:- We are using Visual Studio Code, that’s why we need to run commands. Otherwise, if you have Visual Studio, then you have NuGet package manager console to add entity framework.
So, now run the below two commands to add packages.
- dotnet add package Microsoft.EntityFrameworkCore.SqlServe
- dotnet add package Microsoft.EntityFrameworkCore.Design
Now, run this below command to restore the packages.
- dotnet restore
Let’s move ahead, now we need to install commands for entity framework like Add-Migration, Update-database and etc. So, to do this, just put the below line of code into the csporj file.
Step # 3 => Create DbContext Class.
Now, in this step, we will add a new class with the name of “StudentDbContext”. To do this, create a new folder with the name “StudentDB” and then add a new class with the name “StudentDbContext”. And finally, copy the below code and paste into that file according to your project folder directory.
Now, configure this StudentDbContext class as a service for dependency injection in the project startup class.
Go to project folder structure => then open startup.cs class and then put the below line # 29 of code into the startup.cs class within the ConfigureServices method. As you see the below file.
Now, it’s time to add connection string in our project. Go to project folder structure and then open the appsettings.json file and then copy the connection string code from line # 3 to 5 and then paste into the appsettings.json file as you see below file.
Step # 4 => Create Datebase
Now, in this step, we will create a new database for our project. So, to do this, just run these below commands.
- dotnet ef migrations add InitialCommit
- dotnet ef database update
The above commands will create a new folder in the project with the name of “Migrations” and then the second one command will create a database for our project as you see in the below screenshot.
Step # 5 => create web API
Now, in this step, we will create an API for our project with the name of “StudentController”. To do this just go to Controllers folder, then add a new class with the name of “StudentController”. Now copy the below code and paste into StudentController.cs class as you see in below file.
Now, finally run this project and then put the url like this “http://localhost:5000/api/student ” and test it using the postman. And you will see the output result as you see in below screenshot.
Note:- If you don’t know how to use POSTMAN and How to create CRUD app using Angular 6 and dot net core web APIs, then click here.
Leave a Reply