我正在尝试将我的asp .net标识类连接到自定义sql数据库。我已经在OnModelBuilder方法中定义了映射,如下所示:
var user = modelBuilder.Entity<IdentityUser>().HasKey(u => u.Id).ToTable("User");
user.Property(iu => iu.Id).HasColumnName("Id");
user.Property(iu => iu.UserName).HasColumnName("UserName");
user.Property(iu => iu.PasswordHash).HasColumnName("PasswordHash");
user.Property(iu => iu.SecurityStamp).HasColumnName("SecurityStamp");
user.Property(iu => iu.EmailConfirmed).HasColumnName("EmailConfirmed");
user.Property(iu => iu.LockoutEnabled).HasColumnName("LockoutEnabled");
user.Property(iu => iu.LockoutEndDateUtc).HasColumnName("LockoutEndDateUtc");
user.Property(iu => iu.PhoneNumber).HasColumnName("PhoneNumber");
user.Property(iu => iu.PhoneNumberConfirmed).HasColumnName("PhoneNumberConfirmed");
user.Property(iu => iu.TwoFactorEnabled).HasColumnName("TwoFactorEnabled");
user.Property(au => au.AccessFailedCount).HasColumnName("AccessFailedCount");
user.Property(au => au.TwoFactorEnabled).HasColumnName("TwoFactorEnabled");
//--?--
user.HasMany(u => u.Roles).WithRequired().HasForeignKey(ur => ur.UserId);
user.HasMany(u => u.Claims).WithRequired().HasForeignKey(ur => ur.UserId);
user.HasMany(u => u.Logins).WithRequired().HasForeignKey(ur => ur.UserId);
user.Property(u => u.UserName).IsRequired();
//--
var applicationUser = modelBuilder.Entity<ApplicationUser>().HasKey(au => au.Id).ToTable("User");
applicationUser.Property(au => au.Id).HasColumnName("Id");
applicationUser.Property(au => au.UserName).HasColumnName("UserName");
applicationUser.Property(au => au.Email).HasColumnName("Email");
applicationUser.Property(au => au.PasswordHash).HasColumnName("PasswordHash");
applicationUser.Property(au => au.SecurityStamp).HasColumnName("SecurityStamp");
applicationUser.Property(au => au.EmailConfirmed).HasColumnName("EmailConfirmed");
applicationUser.Property(au => au.ActivationToken).HasColumnName("ActivationToken");
applicationUser.Property(au => au.FirstName).HasColumnName("FirstName");
applicationUser.Property(au => au.LastName).HasColumnName("LastName");
applicationUser.Property(au => au.LockoutEnabled).HasColumnName("LockoutEnabled");
applicationUser.Property(au => au.LockoutEndDateUtc).HasColumnName("LockoutEndDateUtc");
applicationUser.Property(au => au.PhoneNumber).HasColumnName("PhoneNumber");
applicationUser.Property(au => au.PhoneNumberConfirmed).HasColumnName("PhoneNumberConfirmed");
applicationUser.Property(au => au.AccessFailedCount).HasColumnName("AccessFailedCount");
applicationUser.Property(au => au.Discriminator1).HasColumnName("Discriminator1");
applicationUser.Property(au => au.TwoFactorEnabled).HasColumnName("TwoFactorEnabled");
applicationUser.Property(au => au.IdentityRole_Id).HasColumnName("IdentityRole_Id");
//set the primary key for this class. Use .HasMaxLength(40) to specify max length on property
var role = modelBuilder.Entity<IdentityRole>().HasKey(ir => ir.Id).ToTable("Role");
role.Property(ir => ir.Id).HasColumnName("Id");
role.Property(ir => ir.Name).HasColumnName("Name");
var applicationRole = modelBuilder.Entity<ApplicationRole>().HasKey(ar => ar.Id).ToTable("Role");
applicationRole.Property(ir => ir.Id).HasColumnName("Id");
applicationRole.Property(ir => ir.Name).HasColumnName("Name");
//applicationRole.Property(ir => ir.Description).HasColumnName("Description");
var userRole = modelBuilder.Entity<IdentityUserRole>().HasKey(iur => new{iur.UserId, iur.RoleId}).ToTable("UserRole");
userRole.Property(ur => ur.RoleId).HasColumnName("RoleId");
userRole.Property(ur => ur.UserId).HasColumnName("UserId");
var claim = modelBuilder.Entity<IdentityUserClaim>().HasKey(iuc => iuc.Id).ToTable("UserClaim");
claim.Property(iuc => iuc.Id).HasColumnName("Id");
claim.Property(iuc => iuc.ClaimType).HasColumnName("ClaimType");
claim.Property(iuc => iuc.ClaimValue).HasColumnName("ClaimValue");
claim.Property(iuc => iuc.UserId).HasColumnName("UserId");
var login = modelBuilder.Entity<IdentityUserLogin>().HasKey(iul => new { iul.UserId, iul.LoginProvider, iul.ProviderKey }).ToTable("UserLogin");
login.Property(iul => iul.UserId).HasColumnName("UserId");
login.Property(iul => iul.LoginProvider).HasColumnName("LoginProvider");
login.Property(iul => iul.ProviderKey).HasColumnName("ProviderKey");
然而,当我跑步时,它给出错误说
Invalid column name 'Discriminator'.
Invalid column name 'IdentityRole_Id'.
Invalid column name 'Discriminator'.
Invalid column name 'Discriminator'.
如果我理解正确,它会在我的自定义User表中查找这些列名,即使它们未在IdentityUser或我的派生ApplicationUser中定义。
我不确定错误在哪里,我的OnModelCreating方法?
答案 0 :(得分:2)
这不是ASP.NET身份问题。您将基类和派生类,IdentityUser和ApplicationUser分别映射到同一个表“User”,这称为“每个类层次结构的表”,因此EF相应地添加了Descriminator列。 在这种情况下,您应该只使用ApplicationUser类。 这同样适用于IdentityRole和ApplicationRole等。