我将AspNetUsers更改为用户,但是当我向ApplicationUser添加新属性时,它们最终出现在AspNetUsers中

时间:2016-05-01 20:38:18

标签: entity-framework asp.net-identity

在aspnet identity 2中,我这样做是为了删除身份表的AspNet前缀:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {

    }
  protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<IdentityUser>().ToTable("Users");

        modelBuilder.Entity<IdentityRole>().ToTable("Roles");

        modelBuilder.Entity<IdentityUserRole>().ToTable("UserRoles");

        modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaims");

        modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogins");
    }
    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

但是,现在如果我尝试这样做:

public class ApplicationUser : IdentityUser
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> 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;
    }

    public Profile Profile { get; set; }

    public string FullName { get; set; }

}

运行Update-Database后,Users表中没有添加任何字段,但AspNetUsers表是bck并且具有FirstName和FullName字段:

enter image description here

如何将这些字段放在Users表中??

1 个答案:

答案 0 :(得分:1)

您要添加“IdentityUser”2次。一个:IdentityDbContext<ApplicationUser>,一个modelBuilder.Entity<IdentityUser>().ToTable("Users");

替换 modelBuilder.Entity<IdentityUser>().ToTable("Users");  与modelBuilder.Entity<ApplicationUser>().ToTable("Users");