5

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.

Sean Newcome
  • 1,497
  • 2
  • 17
  • 24
  • For the id-issue: http://stackoverflow.com/questions/22721011/aspnet-identity-custom-user-and-custom-role-should-be-simple-what-am-i-missing/22746674#22746674 – Peter Hurtony Mar 30 '14 at 16:47

3 Answers3

3

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

Community
  • 1
  • 1
Sean Newcome
  • 1,497
  • 2
  • 17
  • 24
0

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();
    }
...
Issac
  • 943
  • 1
  • 6
  • 13
0

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.

kat1330
  • 5,134
  • 7
  • 38
  • 61