Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 492
Sean Newcome Avatar asked Oct 21 '22 11:10

Sean Newcome


1 Answers

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

like image 74
Sean Newcome Avatar answered Oct 23 '22 08:10

Sean Newcome