gpt4 book ai didi

asp.net-mvc - UserManager.AddPasswordAsync() 返回 "Name cannot be null or empty"

转载 作者:行者123 更新时间:2023-12-03 03:59:49 25 4
gpt4 key购买 nike

我使用 MVC5 和 ASP.NET 身份框架。

我扩展了由 VS2013 搭建的帐户管理功能。

如果我的用户已向外部登录提供程序注册并想要添加本地密码,则在调用“AddPasswordAsync()”时会出现错误:“名称不能为 null 或为空”

我查看了我的数据库并确认用户名不为空。

这个错误是什么意思?

我的应用程序用户如下所示:

public class Person : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Sex Sex { get; set; }

[Display(Name = "Birth date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime Birthdate { get; set; }
public string EMail { get; set; }
public int PostalCode { get; set; }

public Collection<Race> Races { get; set; }
public Collection<Participation> Participations { get; set; }

public Person()
{
if (string.IsNullOrEmpty(UserName))
{
UserName = EMail;
}
Participations = new Collection<Participation>();
Races = new Collection<Race>();
Birthdate = DateTime.Now;
}
}

最诚挚的问候弗雷德里克

最佳答案

经过多次尝试终于解决了。错误信息是错误的,它来自数据库,意味着用户名字段为空,但数据库需要它。

确保向数据库发送用户名,例如:

 var user = new ApplicationUser { UserName = model.Email, Email = model.Email, Hometown = model.Hometown };

并通过运行 update-database 确保数据库在此之前是最新的。

关于asp.net-mvc - UserManager.AddPasswordAsync() 返回 "Name cannot be null or empty",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21990915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com