Asp.Net Core Identity 多資料庫支援

Asp.Net Core Identity 是.Net自帶的身份認證系統,支援用戶介面 (UI) 登錄功能,並且管理用戶、密碼、配置文件數據、角色、聲明、令牌、電子郵件確認等等。使用Visual Studio創建帶有identity的項目時,使用SqlServer作為預設的資料庫,本文介紹如何改造為多種資料庫支援。

首先,使用Visual Studio 2022創建一個新的Asp.Net Core Web項目,名稱為TestIdentity,選擇身份認證類型為個人賬戶:

創建的項目結構如下:

在Data目錄下保存的是身份認證的DbContext,名稱為ApplicationDbContext,還有基於SqlServer的遷移文件。我們所要做的第一件事情是將SqlServer部分移動到另一個項目中,然後再增加對其它資料庫類型的支援。

現在我們在解決方案中創建一個新的類庫項目,名稱為IdentityEF,在這個項目中安裝包Microsoft.AspNetCore.Identity.EntityFrameworkCore。然後將ApplicationDbContext移動到這個項目中。

然後我們再創建另一個類庫項目,負責SqlServer資料庫的遷移,名稱為IdentityEF.SqlServer,在這個項目中安裝包Microsoft.EntityFrameworkCore.SqlServer和Microsoft.EntityFrameworkCore.Tools,還要增加對IdentityEF的項目引用,然後將TestIdentity中Data目錄下的Migrations子目錄移動到這個項目中:

然後在這個項目中增加新的類DbContextFactory,程式碼如下:

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TestIdentity.Data;

namespace IdentityEF.SqlServer
{
    public class DbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
    {
        public ApplicationDbContext CreateDbContext(string[] args)
        {
            var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
            optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=aspnet-TestIdentity-53bc9b9d-9d6a-45d4-8429-2a2761773502;Trusted_Connection=True;MultipleActiveResultSets=true",
              x => x.MigrationsAssembly("IdentityEF.SqlServer"));
            return new ApplicationDbContext(optionsBuilder.Options);
        }
    }
}

請注意,上面的資料庫名稱與TestIdentity項目中appsettings.json中定義的DefaultConnection是一樣的,這樣,生成的資料庫在TestIdentity中可以直接使用。

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-TestIdentity-53bc9b9d-9d6a-45d4-8429-2a2761773502;Trusted_Connection=True;MultipleActiveResultSets=true"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*"
}

再增加一個依賴注入擴展IdentityEFExtension,方便在Web應用中的引用:

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TestIdentity.Data;

namespace IdentityEF.SqlServer
{
    public static class IdentityEFExtension
    {
        public static IServiceCollection AddIdentityEFSqlServer(this IServiceCollection services, IConfiguration Configuration)
        {
            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"),
                x => x.MigrationsAssembly("IdentityEF.SqlServer")));
            return services;
        }
    }
}

到這裡,改造基本完畢,在Web應TestIdentity項目中,增加對這兩個項目的引用,然後改造Program.cs,將原有的部分注釋掉,增加AddIdentityEFSqlServer:

//// Add services to the container.
//var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
//builder.Services.AddDbContext<ApplicationDbContext>(options =>
//    options.UseSqlServer(connectionString));
//builder.Services.AddDatabaseDeveloperPageExceptionFilter();

builder.Services.AddIdentityEFSqlServer(builder.Configuration);

現在,可以在包管理器中,使用Update-Database創建資料庫。首先,將IdentityEF.SqlServer項目設置為啟動項目,在包管理器中,將預設項目也設置為IdentityEF.SqlServer:


然後運行Update-Database,順利的化,資料庫就生成了。將啟動項目改回到TestIdentity,運行項目,我們可以註冊用戶並進行登錄了。到這裡,針對SqlServer的部分已經從Web項目中分離,現在,我們增加對其它資料庫類型的支援,比如,我們增加Sqlite的支援。

創建一個新的類庫,名稱為IdentityEF.Sqlite,增加程式包Microsoft.EntityFrameworkCore.Sqlite和Microsoft.EntityFrameworkCore.Tools,還要增加對IdentityEF的項目引用,然後增加DbContextFactory:

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using TestIdentityEF.Data;

namespace IdentityEF.Sqlite
{
    public class DbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
    {
        public ApplicationDbContext CreateDbContext(string[] args)
        {
            var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
            optionsBuilder.UseSqlite("DataSource=mydatabase.db;",
                x => x.MigrationsAssembly("IdentityEF.Sqlite"));

            return new ApplicationDbContext(optionsBuilder.Options);
        }
    }
}

還增加依賴注入擴展:

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TestIdentity.Data;

namespace IdentityEF.Sqlite
{
    public static class IdentityEFExtension
    {
        public static IServiceCollection AddIdentityEFSqlite(this IServiceCollection services, IConfiguration Configuration)
        {
            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlite(Configuration.GetConnectionString("IdentityConnection"),
                x => x.MigrationsAssembly("IdentityEF.Sqlite")));
            return services;
        }
    }
}

項目的結構如下:

現在,我們需要生成遷移文件和資料庫。將項目IdentityEF.Sqlite設置為啟動項目,在程式包管理器中,將IdentityEF.Sqlite設置為預設項目:

在程式包管理器中運行:

Add-Migration init

如果一切順利,在項目文件中會增加遷移文件:

然後運行Update-Database,我們會發現,項目中多了db文件:

最後,改造一下Web應用,使其支援Sqlite資料庫,並且可以通過配置文件進行切換。在項目中增加對IdentityEF.Sqlite的引用,然後修改Program.cs:

if (builder.Configuration["DbType"]=="SqlServer")
    builder.Services.AddIdentityEFSqlServer(builder.Configuration);
else
    builder.Services.AddIdentityEFSqlite(builder.Configuration);

在配置文件中使用DbType切換資料庫的類型:

{
  "ConnectionStrings": {
    //"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-TestIdentity-53bc9b9d-9d6a-45d4-8429-2a2761773502;Trusted_Connection=True;MultipleActiveResultSets=true",
    "DefaultConnection": "DataSource=D:\\Asp.Net Core\\TestIdentityEF\\IdentityEF.Sqlite\\mydatabase.db"
  },
  "DbType": "Sqlite",
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*"
}

完整的項目程式碼可以從github下載://github.com/zhenl/TestIdentityEF

Tags: