UserManager.Create(user, password) thowing EntityV

2019-02-10 08:50发布

Anybody know why UserManager.Create(user, password); might be throwing EntityValidationError saying Id is required.

Could it have something to do with my UserManager. It is setup like this:

public class MyAppDb : IdentityDbContext<ApplicationUser, ApplicationRole, string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
    public static MyAppDb Create()
    {
        return new MyAppDb();
    }

    public MyAppDb()
        : base("MyAppDb")
    {
    }
}


public class ApplicationUserStore : UserStore<ApplicationUser, ApplicationRole, string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
    public ApplicationUserStore(MyAppDb context)
        : base(context)
    {
    }
}

public class ApplicationUserManager : UserManager<ApplicationUser, string>
{
    public ApplicationUserManager(IUserStore<ApplicationUser, string> store)
        : base(store)
    {
    }
}

And in the controller like this:

public class AccountController : Controller
{
    public AccountController()
        : this(new ApplicationUserManager(new ApplicationUserStore(new MyAppDb())))
    {
    }

    public AccountController(ApplicationUserManager userManager)
    {
        UserManager = userManager;
        UserManager.PasswordValidator = (IIdentityValidator<string>)new MinimumLengthValidator(8);
        UserManager.UserValidator = new UserValidator<ApplicationUser>(UserManager) { AllowOnlyAlphanumericUserNames = false, RequireUniqueEmail = true };
    }

    public ApplicationUserManager UserManager { get; private set; }

    ...
}

ApplicationUser, ApplicationRole, ApplicationUserLogin, ApplicationUserRole, and ApplicationUserClaim all inherit from the Identity classes respectively.

Anyone see anything wrong? I was able to register users (UserManager.Create()) before I created all the custom Identity classes without the Id issue.

UPDATE: It seems specifying string for the Key has broken the auto-generated Key in the database context. That is my best guess. It now is looking for me to set the Id. I don't want to, I want it to work the way it did before I created the custom fields on the User.

3条回答
在下西门庆
2楼-- · 2019-02-10 09:27

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()

查看更多
The star\"
3楼-- · 2019-02-10 09:28

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();
    }
...
查看更多
一纸荒年 Trace。
4楼-- · 2019-02-10 09:40

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.

查看更多
登录 后发表回答