The web content describes how to create an ASP.NET Core Identity DbContext and database at design-time without a web host, following Microsoft's Clean Architecture recommendations.
Abstract
The article outlines a method for creating a stand-alone class library that encapsulates ASP.NET Core Identity models, DbContext, and Entity Framework Core migrations, enabling the creation of an Identity database through migrations without depending on a web application. This approach is particularly beneficial for projects adhering to Microsoft's Clean Architecture, allowing developers to focus on core entities and infrastructure before dealing with the web app or API layer. The process involves customizing Identity models, creating a design-time factory for the DbContext, and utilizing configuration files for database connections. The article also provides a README template for developers to easily set up and run the necessary commands for creating and applying migrations, ultimately leading to the creation of the Identity database with custom fields.
Opinions
The author advocates for the use of a class library to manage ASP.NET Core Identity independently of a web host, which aligns with the principles of Clean Architecture.
Emphasizing the importance of a README file, the author suggests that thorough documentation is crucial for team collaboration and future development.
The author believes that the outlined approach is advantageous for projects that prioritize the development of core infrastructure before the user interface layer.
By providing a GitHub starter project and related articles, the author implies that these resources are valuable for developers looking to implement similar architectures in their projects.
The author endorses ZAI.chat, an AI service, as a cost-effective alternative to ChatGPT Plus (GPT-4), indicating a preference for this tool based on performance and value.
ASP.NET Core Identity — How to Create DbContext and Database at Design-Time without a Web Host
Discuss a class library that can be used for design-time DbContext creation and database creation for ASP.NET Core Identity using EF Code First, without a web or API project.
Goal
The goal is to create a stand-alone class library whose sole purpose is to host ASP.NET Core Identity models, DbContext and EF Core migrations, and be able to create Identity database by applying migrations without relying on an actual web host. This is particularly useful if you follow Microsoft recommended Clean Architecture and want to develop your core entities and infrastructure first and not worry about the web app or the API (UI layer).
See screenshot below for the folder structure of the class library, “CleanArchitectureCosmosDB.Infrastructure”, that fully supports ASP.NET Core Identity DbContext creation, customized identity models like ApplicationUser which inherits IdentityUser, and migrations management, and database creation through applying migrations.
Screenshot by Author
Let’s break down the class library!
NuGet packages required:
Microsoft.AspNetCore.Identity
Microsoft.AspNetCore.Identity.EntityFrameworkCore
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Tools
Microsoft.Extensions.Configuration
Microsoft.Extensions.Configuration.Json
Step 1 — Customize Identity models and Identity DbContext
Let’s customize our entity type User with our ApplicationUser.cs, which inherits the default IdentityUser and adds FirstName and Last Name to it.
We will then create a customized ApplicationDbContext by deriving from the default IdentityDbContext, and specify that the custom ApplicationUser should be used for entity type User, which relates to the AspNetUsers table in the database that we will cover soon.
Step 2 — Create our design-time factory
According to Microsoft documentation, if a class implementing the IDesignTimeDbContextFactory interface is found in the same project as the derived DbContext, the EF tools bypass the other ways of creating the DbContext and use the design-time factory instead. This is what we rely on when we create our design time DbContext factory.
DesignTimeDbContextFactory.cs
This class implements the IDesignTimeDbContextFactory
The CreateDbContext method returns an ApplicationDbContext, which is our custom DbContext using ApplicationUser defined above
We first build our configuration instance, which contains the connection string to the SQL Server database.
We then build the options by calling AddBaseOptions (explained in the next paragraph), and return an instance of ApplicationDbContext.
Partial ApplicationDbContext.cs
The reason our ApplicationDbContext above is a partial class is that we can add some additional methods that are not tied to the DbContext itself. Here we want to setup SQL Server connection string that will be used.
appsettings.json, where the long waited configuration values are stored in.
Notice the key value matches the config.GetConnectionString(“CleanArchitectureIdentity”) method.
Step 3 — Add README file (the future you and also your co-workers will love you for this!)
The read me file lists all the commands that the next developer will need to get started.
Step 4 — Create migrations
Thanks to the readme file above, we have the command to create the initial migration. We can run the command in the Package Manager Console. In the screenshot below, you can see we’ve successfully used the connection string value from appsettings.json and created one migration for the initial DbContext creation.
Screenshot by author
Step 5 — Apply migrations
Thanks to the readme file again, we have the command to apply the migration(s) and create the initial database. We can run the command in the Package Manager Console. In the screenshot below, you can see we’ve successfully used the connection string value from appsettings.json and applied the migrations.
Screenshot by author
Step 6 — Verify database is created
Hooray! If the connection string works, you should see the database successfully created, with the custom fields added to our AspNetUsers table.
Screenshot by author
Conclusions
We have created out DbContext and our Identity database without a web host. This is particularly helpful if you are working with Clean Architecture and developing core entities first, or if you only want to build a user/token service using ASP.NET Core Identity without knowing when it shall be used.
The sample code in this article is from a GitHub starter project., which uses Clean Architecture to organize the projects. This project features Partitioned Repository Pattern using Azure Cosmos DB to build scalable backend service, REST API, Azure Functions, React SPA, etc.. Feel free to use the whole starter project or part of it to kick start your next exciting adventure!