Create a DotNet API with EntityFramework Core (multiple contexts)

Written by Fa C Shus

May 11, 2020

Problem Space

I write a lot of API tools that I host on the web. Some of the website hosts that my clients use have restrictions like the size of the database, types of databases, and the number of databases. This article will help us solve the restriction on the number of databases. Most of my sites use EntityFrameWork Core (EF) as well as its companion IdentityFramework. Most tutorials and demos create multiple databases. At least one to manage the Identity elements like Users, Roles, Demographics. Then it creates any number of domain databases depending upon how complex the business domain is. The domain is the kind of data that is exposed through the API. This might be teachers and students or clients and projects. In more complex spaces you may have one domain that maintains your catalog and another domain that manages the shopping experience.

Several companion videos accompany this article.

This video will show you how to install docker

Tutorial Requirements (Everything in this article is available with full features and FREE):

  1. Relational Database (The video will help us set one up using docker)
  2. Postman – To test the API (Supporting Video)
  3. DOTNET Core (Supporting Vide
  4. An IDE – I’ll use VS Code, but Visual Studio has a community edition. There are $$$ products available like Rider from JetBrains or IDEA from IntelliJ. These have some bells and whistles, but VS Code has a large community that actively creates and updates extensions for VS Code to replicate just about anything a for cost IDE has.

Now that the requirements are in place we can start building a solution. Our solution will include 3 projects. The projects help us separate the concerns of each part of the solution. This is not strictly required, but let’s try to employ some best practices as we build this solution out. The first project will be the API. This project is the one that exposes the data and allows clients to manipulate and retrieve the data in our database(s). This project will not directly access the database, rather it relies upon the database project to communicate with the database. The final project is the core project and this maintains elements like the logical domain, interfaces, and core implementations of business logic. With this type of structure we can more easily test and refactor the solution when the need arises.

Open a terminal window and execute the following commands. This will create the solution file with three projects. Those three projects will be associated to the solution. The last two commands create a relationship between the projects. The API depends upon the database project, and the database project depends upon the core project.

dotnet new sln -n multi-ctx-solution

dotnet new webapi -o 1.API -n Demo.API
dotnet sln add 1.API

dotnet new classlib -o 3.Core -n Demo.Core
dotnet sln add 3.Core

dotnet new classlib -o 2.Database -n Demo.Database
dotnet sln add 2.Database

dotnet add 1.API/Demo.API.csproj reference 2.Database/
dotnet add 2.Database/Demo.Database.csproj reference 3.Core/

Let’s look at the startup process of a dotnet webapi

If you open up the solution explorer in VS Code, and navigate to the 1.API project, there will be two sentinel files: Program.cs and Startup.cs. Program.cs is the entry point of all dotnet projects and specifically the main method. That main method then create a container by calling the CreateHostBuilder method. This is the root of all dependency injection for dotnet applications and libraries. It is uncommon to manipulate this file until you start advancing to more complex solutions. The CreateHostBuilder method is what consumes the Startup.cs file. This is where the magic begins.

The Startup class contains two important methods. ConfigureServices is what defines the elements that will be injected into the application. The Configure method then starts to use those injected elements and as the name implies, configures them. The goal of this article is less about explaining the magic of how dotnet works and more about implementing an API, so let us move ahead.

Building the Entities

Entities are roughly analogous to tables in the database and that is how this article will define them. We are using the code first strategy with EntityFramework. This allows us to manage our database and improves the portability of our solution. We will define the structure of our database in code and then instruct the database to implement the structure.

Entities are part of the Core project because they help solve the business problem. Our model will be setup to solve the online shopping problem. This will be a simplified model. The model will have Shoppers, Baskets and Products. These entities can be logically grouped together because they define the shopping process. These entities will be added to the ShoppingContext. In addition to shopping we need to manage inventory in the shopping model. The InventoryContext will contain the warehouses.

You May Also Like…

No Results Found

The page you requested could not be found. Try refining your search, or use the navigation above to locate the post.


Submit a Comment

Pin It on Pinterest

Share with Friends...

Share with Friends...