首先,由于User类继承了AbpUser,而AbpUser中的EmailAddress又拥有Required特性,
所以需要重写EmailAddress
[Required(AllowEmptyStrings = true)] //允许空字符串
[StringLength(256)]
public override string EmailAddress { get; set; }
虽然在上面定义了属性可以为空,但是在调用UserManager的CreateAsync方法时,还是会验证邮箱是否为空,此时又需要重写UserManager的CheckDuplicateUsernameOrEmailAddressAsync方法
//————————————————
//版权声明:本文为CSDN博主「从来不喝咖啡」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
//原文链接:https://blog.csdn.net/pxxcsdn/article/details/88574037
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;
}
此时数据库的EmailAddress字段还是不允许为空的,还需要修改Context类,将EmailAddress设置为允许为空
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
//此处省略N行
modelBuilder.Entity<User>().Property(a => a.EmailAddress).IsRequired(false);
然后在程序包管理控制台输入add-migration +此次更新的名字,最后update-database