我正在尝试自定义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();
宣布关键类型似乎超越了崩溃