Добавьте дополнительные свойства в IdentityUser ASP.NET MVC 5
Таблица пользователей
[Table("Users")]
public class User : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<User> 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;
}
[Required]
[Display(Name = "Name")]
public string Name { get; set; }
[Required]
[Display(Name = "Gender")]
public string Gender { get; set; }
[Required]
[Display(Name = "Date of birth")]
public string DateOfBirth { get; set; }
}
MyContext Class
public class myContext : IdentityDbContext<User>
{
public myContext() : base("DefaultConnection")
{
}
public static myContext Create()
{
return new myContext();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<IdentityUser>().ToTable("Users", "dbo");
modelBuilder.Entity<IdentityRole>().ToTable("Roles", "dbo");
modelBuilder.Entity<IdentityUserRole>().ToTable("UserRoles", "dbo");
modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaims", "dbo");
modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogins", "dbo");
}
}
Это создает две таблицы
- AspNetUsers - с дополнительными полями
- Пользователи - с полями по умолчанию
Обратите внимание, что мне здесь не хватает. Как я могу иметь дополнительные поля также в Users
Таблица
1 ответ
Решение
Вам нужно изменить строку в OnModelCreating
в myContext
:
modelBuilder.Entity<IdentityUser>().ToTable("Users", "dbo");
чтобы:
modelBuilder.Entity<User>().ToTable("Users", "dbo");
Сейчас он создает две таблицы, потому что вы определили IdentityUser
тип, который будет использоваться для создания таблицы с именем Users
и одна таблица по умолчанию создается против IdentityUser
тип.