更改身份表的列名(Asp.Net)

时间:2019-04-30 13:39:56

标签: c# asp.net entity-framework model-view-controller asp.net-identity

我想更改所有身份表的列名。我已经更改了表的名称,但是我也想更改列的名称。 到目前为止,这就是我要更改表名称的原因。

请让我知道如何更改列名。例如:

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

此外,如果我更改列名,是否需要对主键和外键做任何事情?

        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");

2 个答案:

答案 0 :(得分:1)

将旧属性的列表和相应的新列名称放在字典中,如下所示。

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);
                }
            }

答案 1 :(得分:1)

在您的onModelCreating类中,执行以下操作:

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");