Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UserManager.CreateAsync does not save added properties in ApplicationUser

I have modified the AspNetUsers table with additional address columns and have followed the steps here except the migration part. I started my project with the database first approach

My identity model has the additional fields

public class ApplicationUser : IdentityUser
{
    public string Email;
    public string Address;
    public string City;
    public string Province;
    public string PostalCode;
}

In the AccountController I build my user like so:

var user = new ApplicationUser() { 
    UserName = model.UserName,
    Email = model.Email,
    Address = model.Address,
    City = model.City,
    Province = model.Province,
    PostalCode = model.PostalCode
};

var result = await UserManager.CreateAsync(user, model.Password);

If I step through the code, I see that the user contains the new properties. However, when it saves, those fields are empty in the database. Is there something I missed or does the code-first migration do a step that haven't covered?

like image 317
sreimer Avatar asked Oct 01 '22 20:10

sreimer


1 Answers

I'm certainly no ASP.NET Identity expert, still trying to get through the basics myself, but two suggestions come to mind.

First, I see you are adding fields to your ApplicationUser class, not properties. This may be the problem. E.g., try changing

public string Email;

into

public string Email { get; set; }

Second, you will want to run the Migrations in order to get these columns added to your database. Although you did seem to say that you already have these columns in your database, so I don't think this step will add anything. (Since you said you started with a database-first approach. I am just mentioning this for the sake of completeness.)

Good luck to you!

like image 114
Funka Avatar answered Oct 03 '22 10:10

Funka