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

asp.net-identity asp.net-mvc asp.net-mvc-5 entity-framework entity-framework-6

Question

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.

1
5
3/28/2014 9:21:09 PM

Accepted Answer

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

3
5/23/2017 10:27:45 AM

Popular Answer

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.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow