question

RonaldRex-2335 avatar image
0 Votes"
RonaldRex-2335 asked ZhiLv-MSFT commented

Modify Identity User properties in Identity Core

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

dotnet-aspnet-core-generaldotnet-entity-framework-core
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

ZhiLv-MSFT avatar image
0 Votes"
ZhiLv-MSFT answered ZhiLv-MSFT commented

Hi @RonaldRex-2335,

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


image.png (18.9 KiB)
image.png (18.9 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Sorry, I should have been more clear but I want to modify the constraints on the columns. I greatly appreciate all of the helpful information you have provided. Thank You !!!!

0 Votes 0 ·

Hi @RonaldRex-2335,

I should have been more clear but I want to modify the constraints on the columns.

Not sure which constrain you want to modify, but you could also refer the above steps and use the Fluent API to configure the IdentityUser model.

The EF Core's Fluent API provides methods for configuring various aspects of your model: Model-wide configuration, Type configuration and Property configuration. For more detail information, see Fluent API Configuration.

Best regards,
Dillion

1 Vote 1 ·