1、Core层: User.cs 实体类中,重写EmailAddress设置可为空类型;
[Required(AllowEmptyStrings = true)] //允许空字符串 public override string EmailAddress { get; set; }
2、Application层: 还有Dto那边EmailAddress 属性也设置允许可空字符串 ;
3、Core层: UserManager类中重写 CheckDuplicateUsernameOrEmailAddressAsync 方法
public override async Task<IdentityResult> CheckDuplicateUsernameOrEmailAddressAsync(long? expectedUserId, string userName, string emailAddress) { var user = (await FindByNameAsync(userName)); if (user != null && user.Id != expectedUserId) { throw new UserFriendlyException(string.Format(L("Identity.DuplicateUserName"), userName)); } if (!string.IsNullOrEmpty(emailAddress)) { user = (await FindByEmailAsync(emailAddress)); if (user != null && user.Id != expectedUserId) { throw new UserFriendlyException(string.Format(L("Identity.DuplicateEmail"), emailAddress)); } } return IdentityResult.Success; }
4、EntityFrameworkCore层: 修改Context类 ,插入这段,设置EmailAddress允许为空;
protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity<User>().Property(a => a.EmailAddress).IsRequired(false); }
5、 更新数据库:打开程序包管理控制台 输入Add-Migration + 更新内容, 然后输入 Update-DataBase ;