Search code examples
asp.netentity-frameworkasp.net-identity

Add column to AspNetUsers table


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.


Solution

  • 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/