简体   繁体   English

ASP.net标识自定义用户模型未被角色,声明和登录表使用

[英]ASP.net Identity custom user model not being used by roles, claims and login table

I am using a custom user model like so: 我正在使用这样的自定义用户模型:

 public class ApplicationUser : IdentityUser
 {
    public string Firstname { get; set; }
    ...
 }

The rest of the tables created by the Identity system are a bit out of sync. Identity系统创建的其余表有点不同步。 A new column is created called ApplicationUser_Id which is set at the foreign key but there is still a UserId which is used a composite primary key. 创建一个名为ApplicationUser_Id的新列,该列在外键处设置,但仍有一个UserId用于复合主键。

生成的表格

Configuration for ApplicationUser model is ApplicationUser模型的配置是

public class ApplicationUserConfiguration : EntityTypeConfiguration<ApplicationUser>
{
    public ApplicationUserConfiguration()
    {
        this.ToTable("AspNetUsers");
        this.Property(u => u.Firstname).IsRequired().HasMaxLength(100);
        this.Property(u => u.Lastname).IsRequired().HasMaxLength(100);
    }
}

How do I tell EF to continue using UserId as the foreign key and get rid of ApplicationUser_Id? 如何告诉EF继续使用UserId作为外键并摆脱ApplicationUser_Id?

Thanks 谢谢

UPDATE: Working as expected until I add custom configuration for ApplicationUser. 更新:按照预期工作,直到我为ApplicationUser添加自定义配置。

DataContext.cs DataContext.cs

modelBuilder.Configurations.Add(new ApplicationUserConfiguration());

ApplicationUserConfiguration.cs ApplicationUserConfiguration.cs

public class ApplicationUserConfiguration : EntityTypeConfiguration<ApplicationUser>
{
    public ApplicationUserConfiguration()
    {
        this.ToTable("AspNetUsers");
        this.Property(u => u.Firstname).IsRequired().HasMaxLength(100);
        this.Property(u => u.Lastname).IsRequired().HasMaxLength(100);
    }
}

I did reproduce the issue then i fixed it by adding modelBuilder.Configurations.Add(new ApplicationUserConfiguration()); 我确实重现了这个问题然后通过添加modelBuilder.Configurations.Add(new ApplicationUserConfiguration())修复了它; before base.OnModelCreating(modelBuilder); 在base.OnModelCreating(modelBuilder)之前; so the function now 所以功能现在

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new ApplicationUserConfiguration());
        base.OnModelCreating(modelBuilder);

    }

You are overriding conventions, so you need to manually map your table. 您正在覆盖约定,因此您需要手动映射表。 Something like this: 像这样的东西:

modelBuilder.Entity<AspNetUserClaims>() 
    .HasRequired(t => t.UserId) 
    .WithOptional(t => t.Claims);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM