Database Migrations

Introduction

We utilize Entity Framework Core (EF Core) as an object-relational mapper (O/RM).

This enables us to work directly with .NET objects that relate directly to our database tables and the underlying schema.

EF Core has two ways of managing the database schema.

In our case we utilize the code-first approach and work with Data Migrations to keep the database up-to-date.

Simply speaking, if we add a new property to the ApplicationUser we can add a new migration to that will update the database table accordingly.

The Login service has already certain migrations for your needs that correspond to all models found in the database.

Current database models correspond to the ones under Models/* directory and have certain attributes that define relationships between other objects.

In addition, certain relationships have to be defined at the ApplicationDbContext with the OnModelCreating method, as attributes do not cover more complex relationships.

Add New Migration

You can add a new migration through the NuGet Package Manager console simply as follows:

Add-Migration {MigrationName} -Context ApplicationDbContext

Moreover, if the above command builds successfully, you can proceed to update the database with the following command:

Update-Database -Context ApplicationDbContext

You can also skip this step and read the next section.

Existing Data and Applying Migrations

For convenience and ease of use, we provide certain data that will be applied the first time you run the Login Service.

The code will also ensure that the database is created and all migrations are applied.

This code lies onto the Program.cs file under the root project directory.

Particularly, this code is inside the Main method and will be invoked as soon as you start running the service and access one of the endpoints or do an API call.

Responsible for this is the helper class is SeedDataHelper under Helpers/* directory.

The helper class will make sure to add certain dummy users and organizations, as well as a few products and link them with the tenants.

More importantly, it will create all necessary Clients, ApiResources and Scopes required for IdentityServer4 and the services (AnalyticsAPI, Portal, VR module) to communicate.

The initial data for IdentityServer4 resources are under Config.cs.

Important

It is strongly suggested that you alter the ClientSecrets for each Client, especially in Production database.