加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 为什么这违反了类型约束?

发布时间:2020-12-16 00:48:16 所属栏目:asp.Net 来源:网络整理
导读:我正在尝试自定义ASP.NET身份3,以便它使用整数键: public class ApplicationUserLogin : IdentityUserLoginint { }public class ApplicationUserRole : IdentityUserRoleint { }public class ApplicationUserClaim : IdentityUserClaimint { }public seale
我正在尝试自定义ASP.NET身份3,以便它使用整数键:
public class ApplicationUserLogin : IdentityUserLogin<int> { }
public class ApplicationUserRole : IdentityUserRole<int> { }
public class ApplicationUserClaim : IdentityUserClaim<int> { }

public sealed class ApplicationRole : IdentityRole<int>
{
  public ApplicationRole() { }
  public ApplicationRole(string name) { Name = name; }
}

public class ApplicationUserStore : UserStore<ApplicationUser,ApplicationRole,ApplicationDbContext,int>
{
  public ApplicationUserStore(ApplicationDbContext context) : base(context) { }
}

public class ApplicationRoleStore : RoleStore<ApplicationRole,int>
{
  public ApplicationRoleStore(ApplicationDbContext context) : base(context) { }
}

public class ApplicationUser : IdentityUser<int>
{
}

public sealed class ApplicationDbContext : IdentityDbContext<ApplicationUser,int>
{
  private static bool _created;

  public ApplicationDbContext()
  {
    // Create the database and schema if it doesn't exist
    if (!_created) {
      Database.AsRelational().Create();
      Database.AsRelational().CreateTables();
      _created = true;
    }
  }
}

这可以编译好,但是会抛出一个运行时错误:

System.TypeLoadException

GenericArguments[0],‘TeacherPlanner.Models.ApplicationUser’,on ‘Microsoft.AspNet.Identity.EntityFramework.UserStore`4[TUser,TRole,TContext,TKey]’ violates the constraint of type parameter ‘TUser’.

UserStore的签名是:

public class UserStore<TUser,TKey>
where TUser : Microsoft.AspNet.Identity.EntityFramework.IdentityUser<TKey>
where TRole : Microsoft.AspNet.Identity.EntityFramework.IdentityRole<TKey>
where TContext : Microsoft.Data.Entity.DbContext
where TKey : System.IEquatable<TKey>

ApplicationUser正是一个IdentityUser< int>。这不是它在寻找什么吗?

解决方法

进入这个问题。这是在startup.cs文件中崩溃。
services.AddIdentity<ApplicationUser,ApplicationIdentityRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders();

services.AddIdentity<ApplicationUser,ApplicationIdentityRole>()
                .AddEntityFrameworkStores<ApplicationDbContext,int>()
                .AddDefaultTokenProviders();

宣布关键类型似乎超越了崩溃

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读