.NET 7 - Minimal APIs with EF Core
.NET 7 - Minimal APIs with EF Core

.NET 7 – Configure Minimal APIs to use EF Core Database

In previous article, I created a minimal API project and we saw what is the default code in the project template. We are going to extend on that code now.

In this article, we are going to configure EF Core DbContext in the minimal APIs and we are going to write APIs to perform CRUD operation on one entity.

Prerequisites

Reorganize Code in API Project

Currently, in the API project, all the code is located in Program.cs file. This file has all the code in this project. Now, let’s divide the code into separate files.

Bootstrappers

Let’s create the folder ‘Bootstrapper‘ and create two classes in it.

  • AppBuilder class which builds the WebApplication instance and returns it. Note that this class also registers the database dependencies.
  • RequestPipelineBuilder class which contains code to build the request pipeline.

The code from these two files is given below.

Project Reference

Now, let’s go to API project, MinimalApiDemo and right click on it and click on Add Project Reference. Select the another project from the solution, which is MinimalApiDemo.DataAccess.

Minimal API Demo – Add project reference to EF Core Data Access Layer

Create APIs

Let’s create the Api folder in the API Project. Then, add a file StudentsApi.cs. This file should contain the logic to register URL routes and the respective handlers. The code snippet shows how this file should look like.

Note that, for this demo, I have created a static class StudentsApi, which contains a static method to RegisterApis. In this same method, I registered four routes:

  • two for get, endpoints,
  • one for post, for creating the student
  • one for delete, for deleting the student

The complete code from this file is given below.

Main Method

Finally, let’s update the main method.

  • It should first create the app instance using AppBuilder
  • Then it should make a call to configure request process pipeline
  • Then the main method can call StudentApi.RegisterApis method to register the APIs
  • Finally, app.Run to make the server up and running.

The code is pretty much same as it is in the default project template. The only difference now is, we have organized the code in different files and that’s why the number of lines of code in the main method is reduced.

Run and Verify

Please make sure that you have executed the commands to generate and apply the migrations.

Once the database is created, we can run the application. It should present us the swagger UI. From this UI, let’s first create a student using POST endpoint. Then use get by id endpoint to make sure that exists.

Next, let’s use delete endpoint to invoke the delete operation. Later, again call get operation to ensure that there are no records. If all of those operations work without any error, we can say that the our minimal API application is working.

Minimal API Demo – Run and Verify

Wrapping Up

Here, I have used static class and static method to register API routes and handlers. I am not sure if there is any other better way for registering APIs.

One thing that I could have done is, I could have extracted the handlers into separate classes. That way, the driving platform could have been made completely independent of the actual application logic. But for this demo I thought it might be too much of work.

Do you have any thoughts about this way of organizing the code ? Do you want to suggest any other way in which the code in minimal API can organized ? Let me know your thoughts.

Leave a Reply Cancel reply