Creating a simple project Web API with VSCode and Entity Framework
Tags: dotnet, csharp, programming
Published: 2021-03-13 18:44:46

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/loo6ube013pjcyex8923.png)

In this article, I will teach how to create API using Visual Studio Code. For this, you need to install VSCode at this [link] (https://code.visualstudio.com/download). 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r5uxnixore8tkae45bll.jpg)


**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j2yeybsdy2jssph675p0.jpg)


**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ydsfri8ivr7h2np7qb7h.jpg)


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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pge3lsbv71a913b0chqx.jpg)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r4nlti6p1z7uyuztaqkw.jpg)

Course

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a44gba37uxkz54z7e3zy.jpg)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fck6jhiyc5ok7x2c0u2o.jpg)


**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i63p5jl63ajk7uc7wvmk.jpg)


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

CategoryController

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/liy6goe4b98skpbpa8hd.jpg)


**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vdnzv5qpw7thwkz71bnz.jpg)

Test

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jpjqvk134ng6xa313h05.jpg)

CoursesController

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/801do0znywjkjjcoyc6f.jpg)

Test

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bq3nzj5igrkoj1szehhd.jpg)



![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6asuy2ma5msmgkynvlzt.jpg)

[Github project] (https://github.com/JessicaNathany/api-core)

**References**

[DataContext in portuguese] (https://docs.microsoft.com/pt-br/ef/core/dbcontext-configuration/)
[Startup Class in portuguese](https://docs.microsoft.com/pt-br/aspnet/core/fundamentals/startup?view=aspnetcore-5.0)
[Startup Class in english](https://docs.microsoft.com/pt-br/aspnet/core/fundamentals/startup?view=aspnetcore-5.0)
[DataContext in english](https://docs.microsoft.com/en-us/dotnet/api/system.data.entity.dbcontext?view=entity-framework-6.2.0)
[Entity Framework tutorial](https://www.entityframeworktutorial.net/entityframework6/dbcontext.aspx)