Renaming a column of the AspNetUsers table doesn't work

23 Views Asked by At

I am new to Asp.Net Core Identity and EF migration.I have created the Identity related tables in my database using code first approach. The AspNetUsers table has a field named Email. I want to rename it to EmailAddress. So in my dbContext object I have the following code.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);
    modelBuilder.HasDefaultSchema("dbo");
    modelBuilder.Entity<ApplicationUser>().Property(p => 
    p.Email).HasColumnName("EmailAddress");
}

Then, I run the migration as shown below: PM> add-migration renameColumn -Context myDbContext PM> Update-Database renameColumn -Context myDbContext

After running it, when I checked my AspNetUsers table, the field remains as Email and didn't get changed to EmailAddress. When I put a breakpoint on my OnModelCreating() function, it never reaches it. I don't get any error either while running the migration. This should be an easy process. Not sure why it's not working!

1

There are 1 best solutions below

0
Brando Zhang On

Firstly make sure you put the OnModelCreating inside the right dbcontxt which you have used for the identity.

I have tried it with the default identity dbcontext with the identity user, it works.

After I running the add-migration command, it generated the migration code like below:

enter image description here

My dbcontext codes:

    public class ApplicationDbContext : IdentityDbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.HasDefaultSchema("dbo");
            modelBuilder.Entity<IdentityUser>().Property(p =>
            p.Email).HasColumnName("EmailAddress");
        }
    }

Result:

enter image description here