【问题标题】:Change column names of identity tables (Asp.Net)更改身份表的列名(Asp.Net)
【发布时间】:2019-09-19 04:34:08
【问题描述】:

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

请告诉我如何更改列名。例如:

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

【问题讨论】:

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


    【解决方案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);
                    }
                }
    

    【讨论】:

    • 这也给了我一个错误 dbModelBuilder does not contain the definition of 'Model'
    • 这是在您的 DB Context 的 OnModelCreating 方法中添加的
    • 谢谢。我收到此错误:dbModelBuilder 不包含“模型”的定义
    • 这根本不起作用。代码中有错误。什么是typeof(User)
    • property.Name 不代表属性。它抛出一个错误 cannot convert from string to System.Linq
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2014-02-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-13
      • 1970-01-01
      • 1970-01-01
      • 2018-09-15
      • 2017-07-25
      • 1970-01-01
      相关资源
      最近更新 更多