DEV Community

Jéssica Nathany
Jéssica Nathany

Posted on • Edited on

Creating a simple project Web API with VSCode and Entity Framework

Alt Text

In this article, I will teach how to create API using Visual Studio Code. For this, you need to install VSCode at this link. In this case, I'm using de NET version 5.0 but you can use the version .NET 3.0.

First step: First of all, before installing the VSCode open the Windows Power Shell and select your directory, the directory where you will create this project API.

Alt Text

Second step: Write in the Power Shell the command dotnet new webapi -o CourseApi. Where the syntax -o is used for creating the directory for your project.

Alt Text

Third step: Open the Visual Studio Code and select your directory, the directory where we created the project Web API. Delete the files WeatherForecast.cs and WeatherForecastController.cs.

Alt Text

Now we let´s create some folders and classes. Lets to create the folder: Model and Data.

Alt Text

We will create the classes Category and Course. How you can see in the image below, we will use the validation in properties using DataAnnotations in fields.

  • Category { Id, Title and CategoryName }
  • Courses {Id, Name, Description, Price, CategoryId and Category }
  • DataContext - for to work EntityFrameworkCore and We will using database in memory.

Category

Alt Text

Course

Alt Text

Now let´s create the class DataContext for we work with Entity Framework Core, so write the terminal console VSCode this command for add package EF in memory run dotnet add package Microsoft.EntityFrameworkCore.InMemory, as in the image below.

Alt Text

Fourth Step: Let´s configure the Startup class for we need to say for our application, that we are work with Entity Framework and configure. The services are registered ConfigureServices and consumed throughout the application through dependency injection (di) or ApplicationServices.

Alt Text

Fifth Step Let´s create the controllers, CartegoryController and CourseController.

CategoryController

Alt Text

Sixth Step Now we need to test our application.
You can run your API with Ctrl+F5 and open the Postman and write the address https://localhost:5001 (HTTPS) or https://localhost:5000. This test we will testing the verbs POST and GET, but you can create other verbs for this application too.

Alt Text

Test

Alt Text

CoursesController

Alt Text

Test

Alt Text

Alt Text

Github project

References

DataContext in portuguese
Startup Class in portuguese
Startup Class in english
DataContext in english
Entity Framework tutorial

Top comments (5)

Collapse
 
celsowm profile image
celsowm • Edited

the third step could be the commands "cd CourseApi" and "code ."

Collapse
 
wilrohde profile image
WIlliam Rohde

This was great, Thank you!

Collapse
 
artydev profile image
artydev

Great thanks you

Collapse
 
jessicanathany profile image
Jéssica Nathany

your wellcome :)

Collapse
 
alsouza93 profile image
Alessandra Souza

Congrats Jéssica!