0

大家好,我正在尝试从标准的 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");

    }
}

不幸的是,当我试图生成我的迁移时。我得到那个错误:

未映射类型“Microsoft.AspNet.Identity.EntityFramework.IdentityRole`2[System.Guid,Easy1WebAPIData.Models.UserRole]”。使用 Ignore 方法或 NotMappedAttribute 数据注释检查该类型是否被显式排除。验证该类型是否被定义为一个类,不是原始的或泛型的,并且不是从 EntityObject 继承的。

有任何想法吗 ?此致!

4

2 回答 2

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();
        }
    }
}
于 2017-05-04T12:20:34.727 回答
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;
于 2017-05-05T09:57:24.910 回答