Add column to AspNetUsers table

asp.net asp.net-identity entity-framework

Question

Workstation setup:

  • Windows 8.1 Pro
  • Visual Studio Pro 2013 v 12.0.40629.00 Update 5
  • .Net v 4.6.01055

Background:

I created my project using Individual Account Authentication and then following a guide I found on Youtube I created an EDMX Data Model from an existing database. I did this to create authentication for my website. I then modified the Default Connection in the web.config file to point to my existing database. When I registered the first user account after modifying the Default Connection it auto-generated all the necessary AspNet tables in my existing database.

Issue:

I need to add a custom column to the AspNetUsers table. I followed Microsoft's guide, found here, but when I get to the step about modifying the Models\IdentityModels.cs file I am unable to do so because the file is missing.

Attempted Resolution:

I've attempted to bypass this by simply adding the column into the SQL database myself and then editing my WebAPI code.

First I edited the RegisterBindingModel class found in the AccountBindingModels.cs file. I added the PeopleID section.

public class RegisterBindingModel
{
    [Required]
    [Display(Name = "User name")]
    public string UserName { get; set; }

    [Required]
    [Display(Name = "Email")]
    public string Email { get; set; }

    [Required]
    [Display(Name = "PeopleID")]
    public string PeopleID { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }
}

Then I edited the Register class found in the AccountController.cs file. I added the line pertaining to PeopleID.

[AllowAnonymous]
    [Route("Register")]
    public async Task<IHttpActionResult> Register(RegisterBindingModel model)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        IdentityUser user = new IdentityUser
        {
            UserName = model.UserName,
            Email = model.Email,
            PeopleID = model.PeopleID,
        };

        IdentityResult result = await UserManager.CreateAsync(user, model.Password);
        IHttpActionResult errorResult = GetErrorResult(result);

        if (errorResult != null)
        {
            return errorResult;
        }

        return Ok();
    }

Now once I add the PeopleID = model.PeopleID, line I get an error message which states "Microsoft.AspNet.Identity.EntityFramework.IdentityUser' does not contain a definition for 'PeopleID'". When I go to the IdentityUser.cs file it is locked and will not allow me to edit it.

1
5
5/5/2017 4:44:42 PM

Accepted Answer

I ended up restarting my project. I updated everything before I made any changes and did not create an EDMX model. I simply changed the default connection string to point to my live database. Once I did all that I found the IdentityModels.cs file and was then able to modify it following this guide:

https://blogs.msdn.microsoft.com/webdev/2013/10/16/customizing-profile-information-in-asp-net-identity-in-vs-2013-templates/

0
5/10/2017 1:52:58 PM

Popular Answer

The error means that you didn't add the column to the IdentityUser class / table in the model (edmx).

Problem is, you normally can't just extend IdentityUser, you'll need to inherit from IdentityUser. Normally, because in your case I suspect that the AspNetUsers table is part of the edmx. So I guess you can add a column there to solve this. But I'm not sure.

With code first (like mentioned in the Microsoft guide) you should create a class called ApplicationUser which inherits IdentityUser. And add the property there:

public class ApplicationUser : IdentityUser
{
    [Required]
    public string PeopleID { get; set; }
}

And then target ApplicationUser (instead of IdentityUser) in your code:

var user = new ApplicationUser
{
    UserName = model.UserName,
    Email = model.Email,
    PeopleID = model.PeopleID,
};


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