简体   繁体   中英

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?

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. Eg, 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM