You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.Net Core 3.1中LinqToDB.Identity UserStore泛型约束异常问题

解决LinqToDB.Identity UserStore泛型约束违反的TypeLoadException问题

这个异常的核心原因是你的AppUser类继承的基类不符合LinqToDB.Identity中UserStore的泛型约束要求。LinqToDB.Identity库提供了自己的Identity实体基类,必须使用这些基类才能和它的Store实现兼容,而不是直接使用微软ASP.NET Core Identity的原生基类。

具体问题分析

你的AppUser继承自Microsoft.AspNetCore.Identity.IdentityUser<int>,但LinqToDB.Identity的UserStore<TUser, TRole, TContext, TKey, TUserClaim, TUserRole, TUserLogin, TUserToken, TRoleClaim>要求TUser必须继承自LinqToDB.Identity.IdentityUser<TKey>(这里TKey是int)。原生的IdentityUser不满足这个约束,所以触发了TypeLoadException。

解决方案步骤

1. 修改AppUser的基类

将AppUser的基类从IdentityUser<int>替换为LinqToDB.Identity.IdentityUser<int>,同时确保引用的是LinqToDB.Identity命名空间下的类:

using DevPlatform.Core.Entities;
using LinqToDB.Identity;
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace DevPlatform.Core.Domain.Identity
{
    // 替换基类为LinqToDB.Identity.IdentityUser<int>
    public class AppUser : IdentityUser<int>, IEntity
    {
        public DateTime CreatedDate { get; set; }
        public DateTime? ModifiedDate { get; set; }
        public int? CreatedBy { get; set; }
        public int? ModifiedBy { get; set; }
        public int? StatusId { get; set; }
        [Required]
        [ForeignKey("UserDetail")]
        public int DetailId { get; set; }
        public virtual AppUserDetail UserDetail { get; set; }
    }
}

2. 统一IdentityRole的泛型参数

在AddIdentity配置中,建议明确指定泛型参数为LinqToDB.Identity.IdentityRole<int>,保持和AppUser的主键类型(int)一致,避免潜在的类型不匹配问题:

public static void AddDevPlatformAuthentication(this IServiceCollection services, IConfiguration configuration) {
    // 修改为IdentityRole<int>,和AppUser的主键类型对齐
    services.AddIdentity<AppUser, LinqToDB.Identity.IdentityRole<int>>(options => {
        options.Password.RequireDigit = true;
        options.Password.RequiredLength = 4;
        options.Password.RequireNonAlphanumeric = false;
        options.Password.RequireUppercase = true;
        options.Password.RequireLowercase = false;
        options.User.RequireUniqueEmail = true;
        //TODO
        //options.User.RequireUniqueEmail = true;
        //options.SignIn.RequireConfirmedEmail = true;
        //options.Lockout.MaxFailedAccessAttempts = 5;
        //options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(3);
    }).AddLinqToDBStores(new DefaultConnectionFactory())
     .AddDefaultTokenProviders();
    services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie();
    // Uncomment the following lines to enable logging in with third party login providers
    JwtTokenDefinitions.LoadFromConfiguration(configuration);
    services.ConfigureJwtAuthentication();
    services.ConfigureJwtAuthorization();
}

3. 验证其他Identity相关类(如AppRole)

如果你自定义了AppRole类,同样需要确保它继承自LinqToDB.Identity.IdentityRole<int>,而不是原生的IdentityRole,这样才能和LinqToDB的Store完美兼容。

为什么这样修改能解决问题?

LinqToDB.Identity库的UserStore实现依赖于自身提供的IdentityUser和IdentityRole基类,这些基类已经集成了LinqToDB的实体映射特性(比如表名、列名的配置),同时满足Store类的泛型约束条件。使用原生ASP.NET Core Identity的基类无法满足这些约束,因此会触发类型加载异常。

内容的提问来源于stack exchange,提问作者Ismail Dogan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 20:12:37