Modify Identity User properties in Identity Core

Ronald Rex 1,666 Reputation points
2021-11-08T22:04:38.507+00:00

If there is a preexisting property in identy user like email, how do i modify it? Thanks!!!

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
697 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,154 questions
0 comments No comments
{count} votes

Accepted answer
  1. Zhi Lv - MSFT 32,011 Reputation points Microsoft Vendor
    2021-11-09T08:52:29.967+00:00

    Hi @Ronald Rex ,

    By default, the Identity User contains the following properties:

    147686-image.png

    Do you mean you want to modify the property name, for example, modify the Email to UserEmail? If that is the case, you could use the Fluent API and HasColumnName() method to change the property name, code like this:

    public class ApplicationDbContext : IdentityDbContext  
    {  
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)  
            : base(options)  
        {   
        }  
        protected override void OnModelCreating(ModelBuilder modelBuilder)  
        {  
            base.OnModelCreating(modelBuilder);  
      
            modelBuilder.Entity<IdentityUser>(  
                iu => iu.Property(c => c.Email).HasColumnName("UserEmail")  
                );  
        }  
    }   
    

    Then, use the following migration commands to enable migration:

    add-migration changeIdentityUseremail  
    
    update-database  
    

    The generate migration file as below:

    public partial class changeIdentityUseremail : Migration  
    {  
        protected override void Up(MigrationBuilder migrationBuilder)  
        {  
            migrationBuilder.RenameColumn(  
                name: "Email",  
                table: "AspNetUsers",  
                newName: "UserEmail");  
        }  
    
        protected override void Down(MigrationBuilder migrationBuilder)  
        {  
            migrationBuilder.RenameColumn(  
                name: "UserEmail",  
                table: "AspNetUsers",  
                newName: "Email");  
        }  
    }  
    

    And the result like this: we can see the Email column has been changed to UserEmail

    147619-image.png

    Besides, if you mean you want to add custom data to the Identity User table, you could refer the following articles:

    Add, download, and delete custom user data to Identity in an ASP.NET Core project

    Identity model customization in ASP.NET Core


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion


0 additional answers

Sort by: Most helpful