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

前端之家收集整理的这篇文章主要介绍了asp.net – 为什么这违反了类型约束?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试自定义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();

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

原文链接:https://www.f2er.com/aspnet/253224.html

猜你在找的asp.Net相关文章