avatarM. Ramadhan

Summary

The provided context outlines the process of integrating ASP.NET Core Identity tables into an existing Blazor Server project's database using a single DbContext and connection string.

Abstract

The article is the thirteenth in a series aimed at guiding developers through practical Blazor Server application projects. It focuses on the integration of Identity tables into the existing database of the BookApp project. The steps include downloading the project, creating the BookDB database, installing ASP.NET Core Identity, modifying the code to use a single DbContext (BookAppContext) and connection string (DefaultConnection), and generating the Identity database through migration. The process ensures that the project maintains a unified database structure and simplifies the management of user authentication and authorization within the Blazor Server application.

Opinions

  • The author emphasizes the practicality of the guide for developers looking to streamline their project's database structure by using a single DbContext and connection string.
  • The guide suggests that having a single database, DbContext, and connection string simplifies the project setup and maintenance, which is beneficial for the project's scalability and manageability.
  • The article implies that integrating Identity tables into an existing database is a preferable approach compared to having a separate identity database, as it centralizes data management.
  • The author provides a list of related articles, indicating a comprehensive approach to teaching Blazor Server application development and suggesting that the series is a valuable resource for learning.
  • By providing step-by-step instructions, code snippets, and screenshots, the author conveys a commitment to clarity and usability in technical writing, aiming to enhance the reader's learning experience.

Blazor Server Project #13: How to Generate Identity Tables

An easy guide to integrating Identity tables into the database of the existing project

Table of Contents

Photo by Brett Jordan on Unsplash

This article is the thirteenth in a series covering the Blazor Server Project: (1) How to create a CRUD operation using Dapper (2) Building a dropdown list involves a 1:N relationship (3) How to implement a checkbox list involving an M:N relationships (4) Understanding URL routing and navigation (5) Creating and using page layout (6) How to create a reusable modal dialog component (7) Practical guide to making a master-detail page (8) Master-detail page using dynamic query (9) How to avoid SQL injection attacks (10) Hiding/showing HTML elements (11) Migrate to ASP.NET Core 6.0 (12) Installing ASP.NET Core Identity (13) Integrating Identity tables into the existing project database (14) Authentication and authorization (15) Role-based authorization (16) How to implement Google Authentication (17) Facebook Authentication and Authorization (18) How to configure Twitter Authentication (19) How to Customize Password Policy (20) Account Lockout Policy

These articles are for anyone who wants to learn how to build Blazor Server applications in a practical approach through some sample projects. It will be straightforward if you have some experience with C#, HTML, CSS, and SQL.

Introduction

In the Blazor Server Project #12 article, we discussed installing ASP.NET Core Identity and, by default, generating a separate identity database from the existing project database.

For now, we will cover how to add identity tables to the database of the existing project. So, the project has only one database, one DbContext, and one connection. The steps are: ⦁ download and open the BookApp project ⦁ create the BookDB database ⦁ install ASP.NET Core Identity ⦁ modify the code ⦁ generate Identity database.

Downloading and Opening the BookApp Project

  • Please download the Blazor Server Project #11 via the link below.
  • Open the project in Visual Studio 2022.

Creating the BookDB Database

Please read the Blazor Server Project #8 article in detail.

Installing ASP.NET Core Identity

The steps are similar to the Blazor Server Project #12.

  • After selecting the options in the Add Identity dialog,
Figure 1 Screenshot of Add Identity dialog

click + button to create a data context class.

Figure 2 Screenshot of Add Data Context dialog
  • Accept the default BookAppContext data context and click Add button.

Modifying the Code

The project must have only one database, one DbContext, and one connection string. You must update five files and delete one file. See Figure 3.

Figure 3 Five files to update and one file to delete

You can use the Find and Replace feature to update the code by selecting the menu: Edit > Find and Replace > Replace in Files.

(a) Use BookAppContext as the one and only DBContext

  • In the Startup.cs file, replace AppContext with BookAppContext.
  • Delete the AppContext.cs file.

(b) Use "DefaultConnection" as the only connection string

  • In the IdentityHostingStartup.cs, serviceDepencies.json and serviceDependencies.local.json files, replace BookAppContextConnection with DefaultConnection.
  • In the appsetting.json file, delete the "BookAppContextConnection" connection string.
  • In the Startup.cs file, delete the statement: services.AddDbContext….

Generating Identity Database

The steps are creating and applying migration.

Creating migration

There is only one DBContext. The migration command does not require the Context parameter. Type the command below in the Package Manager Console.

Add-Migration CreateIdentitySchema

Figure 4 Command to create the migration

The results are two files in the Migrations folder.

Figure 5 Migration files

Applying migration

  • In the Package Manager Console, type Update-Database.
Figure 6 Command to create Identity tables
  • The result is seven identity tables and one migration history added to the BookDB database.
Figure 7 Identity tables

Summary

With a slight modification so that the project has only one DBContext with one connection string, you can integrate the Identity tables into your existing project database.

Hopefully beneficial. Feel free to comment.

References

Blazor
Identity
Security
Migration
Recommended from ReadMedium