更改身份表的列名(ASP.NET)



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

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

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

在您的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");

将旧属性的列表和相应的新列名称列表如下所示。

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

相关内容

  • 没有找到相关文章

最新更新