Learn AspNet
Learn AspNet

Reputation: 1612

Change column names of identity tables (Asp.Net)

I would like to change column names of all identity tables. I have already changed the names of the table but I would like to change the column names as well. This is what I have so far to change table names.

Please let me know how can I change column names. For example:

Id = lngUserID
Email = strUserEmail
EmailConfirmed = strUserEmailConfirmed
....

Also, if I change column names, do I have to do anything regarding primary and foreign keys?

        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<ApplicationUser>().ToTable("tblUsers");
        modelBuilder.Entity<IdentityRole>().ToTable("tblRoles");
        modelBuilder.Entity<IdentityUserClaim>().ToTable("tblUserClaims");
        modelBuilder.Entity<IdentityUserLogin>().ToTable("tblUserLogins");
        modelBuilder.Entity<IdentityUserRole>().ToTable("tblUserRoles");

Upvotes: 0

Views: 1010

Answers (2)

Nirjhar Vermani
Nirjhar Vermani

Reputation: 1245

In your onModelCreating Class, do the following:

modelBuilder.Entity<ApplicationUser>().Property(p => p.Id).HasColumnName("strUserID");
modelBuilder.Entity<ApplicationUser>().Property(p =>p.Email).HasColumnName("strUserEmail");
modelBuilder.Entity<ApplicationUser>().Property(p => p.EmailConfirmed).HasColumnName("strUserEmailConfirmed");

Upvotes: 2

Yared
Yared

Reputation: 2452

Put the list of old properties and the corresponding new column name in the a dictionary like below.

IDictionary<string, string> UserTableColumns = new Dictionary<string, string>() {
            {"Id",  "lngUserID"}, {"Email",  "strUserEmail"},{"EmailConfirmed", "strUserEmailConfirmed"}};

            foreach (var property in typeof(ApplicationUser).GetProperties())
            {
                if (UserTableColumns.ContainsKey(property.Name))
                {
                    builder.Entity<ApplicationUser>().Property(property.Name).HasColumnName(property.Name);
                }
            }

Upvotes: 1

Related Questions