为了完全自定义ASP.NET Identity框架,我从所有六个Identity类继承了类:
public class VigilUserRole : IdentityUserRole<Guid>
public class VigilUserLogin : IdentityUserLogin<Guid>
public class VigilUserClaim : IdentityUserClaim<Guid>
public class VigilUser : IdentityUser<Guid, VigilUserLogin, VigilUserRole, VigilUserClaim>
public class VigilRole : IdentityRole<Guid, VigilUserRole>
public class VigilDataContext : IdentityDbContext<VigilUser, VigilRole, Guid, VigilUserLogin, VigilUserRole, VigilUserClaim>
项目中没有其他内容,我尝试添加初始迁移。所有默认的AspNet*
表都是迁移中包含的唯一表。
public override void Up()
{
CreateTable(
"dbo.AspNetRoles",
c => new
{
Id = c.Guid(nullable: false),
Name = c.String(nullable: false, maxLength: 256),
})
.PrimaryKey(t => t.Id)
.Index(t => t.Name, unique: true, name: "RoleNameIndex");
CreateTable(
"dbo.AspNetUserRoles",
c => new
{
UserId = c.Guid(nullable: false),
RoleId = c.Guid(nullable: false),
VigilUserRoleId = c.Guid(nullable: false),
})
.PrimaryKey(t => new { t.UserId, t.RoleId })
.ForeignKey("dbo.AspNetRoles", t => t.RoleId, cascadeDelete: true)
.ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
.Index(t => t.UserId)
.Index(t => t.RoleId);
CreateTable(
"dbo.AspNetUsers",
c => new
{
Id = c.Guid(nullable: false),
Email = c.String(maxLength: 256),
EmailConfirmed = c.Boolean(nullable: false),
PasswordHash = c.String(),
SecurityStamp = c.String(),
PhoneNumber = c.String(),
PhoneNumberConfirmed = c.Boolean(nullable: false),
TwoFactorEnabled = c.Boolean(nullable: false),
LockoutEndDateUtc = c.DateTime(),
LockoutEnabled = c.Boolean(nullable: false),
AccessFailedCount = c.Int(nullable: false),
UserName = c.String(nullable: false, maxLength: 256),
})
.PrimaryKey(t => t.Id)
.Index(t => t.UserName, unique: true, name: "UserNameIndex");
CreateTable(
"dbo.AspNetUserClaims",
c => new
{
Id = c.Int(nullable: false, identity: true),
UserId = c.Guid(nullable: false),
ClaimType = c.String(),
ClaimValue = c.String(),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
.Index(t => t.UserId);
CreateTable(
"dbo.AspNetUserLogins",
c => new
{
LoginProvider = c.String(nullable: false, maxLength: 128),
ProviderKey = c.String(nullable: false, maxLength: 128),
UserId = c.Guid(nullable: false),
})
.PrimaryKey(t => new { t.LoginProvider, t.ProviderKey, t.UserId })
.ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
.Index(t => t.UserId);
}
我甚至尝试重写OnModelCreating
方法,明确忽略Identity
表并注册Vigil
类。
protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
{
modelBuilder.Ignore(new Type[] { typeof(IdentityUser), typeof(IdentityRole), typeof(IdentityUserClaim), typeof(IdentityUserRole), typeof(IdentityUserLogin) });
modelBuilder.RegisterEntityType(typeof(VigilUser));
modelBuilder.RegisterEntityType(typeof(VigilUserRole));
modelBuilder.RegisterEntityType(typeof(VigilUserClaim));
modelBuilder.RegisterEntityType(typeof(VigilUserLogin));
modelBuilder.RegisterEntityType(typeof(VigilRole));
base.OnModelCreating(modelBuilder);
}
我遗漏了哪些Vigil
类在迁移中未被识别,但是Identity
类(映射到AspNet
表)被包含在内?我甚至尝试用TKey
替换int
类型,但收到相同的结果。
也可以从my sandbox repo下载解决方案,其中包含解决此问题的各种失败尝试的历史记录。
答案 0 :(得分:1)
您的ApplicationDbContext继承自IdentityDbContext。 ApplicationDbContext最后一行的OnModelCreating方法内部(base.OnModelCreating(modelBuilder)) 您调用包含
的基类的OnModelCreating方法(源here)modelBuilder.Entity<TUser>().ToTable("AspNetUsers");
modelBuilder.Entity<TUserRole>().HasKey(r => new { r.UserId, r.RoleId })
.ToTable("AspNetUserRoles");
modelBuilder.Entity<TUserLogin>()
.HasKey(l => new { l.LoginProvider, l.ProviderKey, l.UserId })
.ToTable("AspNetUserLogins");
modelBuilder.Entity<TUserClaim>()
.ToTable("AspNetUserClaims");
modelBuilder.Entity<TRole>()
.ToTable("AspNetRoles");
所以不管你上面做了什么,都要覆盖它。
尝试在自定义代码之前将此行移到顶部。
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<VigilUser>().ToTable("VigilUser");
modelBuilder.Entity<VigilRole>().ToTable("VigilRole");
modelBuilder.Entity<VigilUserRole>().ToTable("VigilUserRole");
modelBuilder.Entity<VigilUserClaim>().ToTable("VigilUserClaim");
modelBuilder.Entity<VigilUserLogin>().ToTable("VigilUserLogin");
}