Section11: Setting up the connection string (Restful API in ASP .Net Core)

 Section11: Setting up the connection string


Introduction:


In the previous section, we have seen how we configure DBContext class so that Entity Framework can communicate between the database and the Models. We have also configured the DBSet class that represents a table in the database.

Just by creating the DBContext class will not help the service (ASP .Net) know that your service has a DBContext created. We have to tell the service about the our custom DBContext, the database type we use and link those data to the service.

For this reason, we have to add the DBContext details to the ConfigureService method where we do our configurations.   

In .Net Core 6, we don't have a startup.cs class and ConfigureService method. We have to add it in Program.cs file.

The connection that we are adding now is just a string; however, this string should come from configuration files such as appsettings.json. For now, let us just keep it simple.

Let us code it now: