【问题标题】:I changed AspNetUsers to Users, but when I add new properies to ApplicationUser, they end up in AspNetUsers我将 AspNetUsers 更改为 Users,但是当我向 ApplicationUser 添加新属性时,它们最终会出现在 AspNetUsers
【发布时间】:2016-05-02 05:25:32
【问题描述】:

在 aspnet 身份 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 字段:

如何让这些字段出现在用户表中..?

【问题讨论】:

  • 你能展示你所有的 IdentityDbContext 类,而不仅仅是 OnModelCreating 吗?
  • 您还需要通过添加modelBuilder.Entity&lt;ApplicationUser &gt;().ToTable("Users");ApplicationUser 映射到与IdentityUser 相同的表

标签: entity-framework asp.net-identity


【解决方案1】:

您添加了 2 次“IdentityUser”。一个在这里:IdentityDbContext&lt;ApplicationUser&gt; 一个在这里modelBuilder.Entity&lt;IdentityUser&gt;().ToTable("Users");

替换 modelBuilder.Entity&lt;IdentityUser&gt;().ToTable("Users"); modelBuilder.Entity&lt;ApplicationUser&gt;().ToTable("Users");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-05
    • 2021-10-24
    • 2017-10-04
    • 2015-03-26
    • 2021-09-06
    • 2019-03-25
    • 2018-08-13
    • 1970-01-01
    相关资源
    最近更新 更多