大家好我正试图继承标准的AspNet表。
我的目的是使用Guid作为主键并扩展基本的AspNetUser 有一些属性。此外,我想重命名表名。
现在就像:
public class User : IdentityUser<Guid, UserLogin, UserRole, UserClaim>
{
public User() { }
public User(string userName)
{
this.UserName = userName;
}
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<User, Guid> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
我的DbContext看起来像这样:
public class CustomDBContext : IdentityDbContext<User, Role, Guid, UserLogin, UserRole, UserClaim>
{
public static CustomDBContext Create()
{
return new CustomDBContext();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<IdentityRole<Guid, UserRole>>().ToTable("Roles");
modelBuilder.Entity<IdentityUserLogin<Guid>>().ToTable("UserLogins");
modelBuilder.Entity<IdentityUserClaim<Guid>>().ToTable("UserClaims");
modelBuilder.Entity<IdentityUserRole<Guid>>().ToTable("UserRoles");
modelBuilder.Entity<IdentityUser<Guid, UserLogin, UserRole, UserClaim>>().ToTable("Users");
}
}
不幸的是,Iam尝试生成我的迁移。我收到了这个错误:
类型&#39; Microsoft.AspNet.Identity.EntityFramework.IdentityRole`2 [System.Guid,Easy1WebAPIData.Models.UserRole]&#39;没有映射。使用Ignore方法或NotMappedAttribute数据批注检查未明确排除类型。验证类型是否已定义为类,不是原始类型还是通用类型,并且不从EntityObject继承。
有什么想法吗?最好的问候!
答案 0 :(得分:1)
我认为您只需要在OnModelCreating
方法中映射扩展模型。我在我的一个项目上做了以下工作,它在迁移方面做得很好。
public class ApplicationUser : IdentityUser<Guid, CustomUserLogin, CustomUserRole, CustomUserClaim> { }
public class CustomUserRole : IdentityUserRole<Guid> { }
public class CustomUserClaim : IdentityUserClaim<Guid> { }
public class CustomUserLogin : IdentityUserLogin<Guid> { }
public class CustomRole : IdentityRole<Guid, CustomUserRole> { }
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, CustomRole, Guid, CustomUserLogin, CustomUserRole, CustomUserClaim>
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ApplicationUser>().ToTable("Users");
modelBuilder.Entity<CustomRole>().ToTable("Roles");
modelBuilder.Entity<CustomUserRole>().ToTable("UserRoles");
modelBuilder.Entity<CustomUserLogin>().ToTable("UserLogins");
modelBuilder.Entity<CustomUserClaim>().ToTable("UserClaims");
}
}
另外,如果您使用Guid的密钥,那么在对象的构造函数中设置Id是很常见的。
public class ApplicationUser : IdentityUser<Guid, CustomUserLogin, CustomUserRole, CustomUserClaim>
{
public ApplicationUser()
{
public ApplicationUser()
{
Id = Guid.NewGuid();
}
}
}
答案 1 :(得分:0)
最后它奏效了!致@Phil Thomas
modelBuilder.Entity<User>().ToTable("User").Property(p => p.Id).HasColumnName("UserID");
modelBuilder.Entity<User>().Property(p => p.Email).HasColumnName("EmailAddress");
modelBuilder.Entity<UserRole>().HasKey(r => new { r.RoleId, r.UserId });
modelBuilder.Entity<UserRole>().ToTable("UserRole");
modelBuilder.Entity<UserRole>().Property(r => r.UserId).HasColumnName("UserID");
modelBuilder.Entity<UserRole>().Property(r => r.RoleId).HasColumnName("RoleID");
modelBuilder.Entity<UserLogin>().ToTable("UserLogin");
modelBuilder.Entity<UserLogin>().Property(r => r.UserId).HasColumnName("UserID");
modelBuilder.Entity<UserClaim>().ToTable("UserClaim");
modelBuilder.Entity<UserClaim>().Property(r => r.Id).HasColumnName("UserClaimID");
modelBuilder.Entity<Role>().HasKey<Guid>(r => r.Id);
modelBuilder.Entity<Role>().ToTable("Role");
modelBuilder.Entity<Role>().Property(r => r.Id).HasColumnName("RoleID");
Configuration.ProxyCreationEnabled = false;
Configuration.LazyLoadingEnabled = false;