Код: Выделить всё
RegistrationDateЗатем мне нужно было изменить некоторые поля пользователя по умолчанию (
Код: Выделить всё
UserNameКод: Выделить всё
public class ExtendedIdentityUser : IdentityUser
{
public override string UserName { get; set; } = string.Empty;
public override string Email { get; set; } = string.Empty;
public DateTime RegistrationDate { get; set; }
}
< /code>
Моя пользовательская модель карты модели: < /p>
public class ExtendedIdentityUserMap : IEntityTypeConfiguration
{
public void Configure(EntityTypeBuilder builder)
{
// Properties
builder.Property(u => u.UserName).HasColumnName("UserName").IsRequired();
builder.Property(u => u.Email).HasColumnName("Email").IsRequired();
builder.Property(u => u.RegistrationDate).IsRequired();
// Default values
builder.Property(u => u.RegistrationDate).HasDefaultValueSql("GETDATE()")
}
}
< /code>
ApplicationDbContextКод: Выделить всё
public class ApplicationDbContext(DbContextOptions options) : IdentityDbContext(options)
{
public DbSet ExtendedIdentityUsers { get; set; }
// another tables
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.ApplyConfiguration(new ExtendedIdentityUserMap());
// another mappings
}
}
< /code>
Generated migrations code looks good:
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn(
name: "UserName",
table: "AspNetUsers",
type: "nvarchar(256)",
maxLength: 256,
nullable: false,
defaultValue: "",
oldClrType: typeof(string),
oldType: "nvarchar(256)",
oldMaxLength: 256,
oldNullable: true);
migrationBuilder.AlterColumn(
name: "Email",
table: "AspNetUsers",
type: "nvarchar(256)",
maxLength: 256,
nullable: false,
defaultValue: "",
oldClrType: typeof(string),
oldType: "nvarchar(256)",
oldMaxLength: 256,
oldNullable: true);
}
< /code>
Changes are appiled successfully to my local DB - columns UserNameМой вопрос: что я отсутствующий? Почему поля имени пользователя и электронной почты пусты для моего объекта пользователя? Редактировать
Я изучил его глубоко, и я обнаружил, что пользователи свойств в microsoft.aspnetcore.identity.entityframeworkcore.userstore уже содержит список пользователей без заполнения email и Имя пользователя поля. Вот почему метод найдий byemailAsync () из usermanager возвращает не заполненные данные, а также использует класс Userstore для получения userdata.
Подробнее здесь: https://stackoverflow.com/questions/793 ... me-and-ema
Мобильная версия