我正在尝试自定义ASP.NET Identity 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, ApplicationDbContext, int>
{
public ApplicationRoleStore(ApplicationDbContext context) : base(context) { }
}
public class ApplicationUser : IdentityUser<int>
{
}
public sealed class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, 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
UserStore
的签名为:'Microsoft.AspNet.Identity.EntityFramework.UserStore`4[TUser,TRole,TContext,TKey]'
但是,GenericArguments[0], 'TeacherPlanner.Models.ApplicationUser',违反了类型参数 'TUser' 的约束条件。
public class UserStore<TUser, TRole, TContext, 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>
。这不就是它要找的吗?