UserManager.Create(user, password) thowing EntityValidationError saying Id is required?

人盡茶涼 提交于 2019-11-30 14:42:56
Sean Newcome

It appears specifying string tells the UserStore that you will provide the Id. This issue went away when I solved the following issue Is there a way to create a custom User and Role without specifying the TKey on IdenitityUser, IdentityRole, and IdentityDbContext?

-OR-

Just give it an id like this: Guid.NewGuid().ToString()

Another thing you can try is to add the id on a parameterless constructor of ApplicationUser:

public class ApplicationUser :  IdentityUser<string, IdentityUserLogin, ApplicationUserRole, IdentityUserClaim>, IUser
{
    public ApplicationUser()
        : base()
    {
        this.Id = Guid.NewGuid().ToString();
    }
...

Method Create() requires ID.

I just inherited IdentityUser like this:

public class ApplicationUser : IdentityUser
{
  // Rest of your implmentation...
}

When I create new instance of ApplicationUser it will contain ID.

public static ApplicationUser GetUser(this UserViewModel model)
    {
        var applicationUser = new ApplicationUser
        {
            DisplayName = model.DisplayName,
            Email = model.Email,
            EmailConfirmed = model.EmailConfirmed,
            UserName = model.UserName,
            Description = model.Description,
            LastSignOn = model.LastSignOn
        };

        return applicationUser;
    }

GetUser() method will convert UserViewModel which doesn't have ID ApplicationUser with populated ID.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!