覆盖默认标识表名称

覆盖默认标识表名称

本文介绍了覆盖默认标识表名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想重命名默认的身份表名称:

I would like to rename the default identity table names:

  • AspNetRoles
  • AspNetUserClaims
  • AspNetUserLogins
  • AspNetUserRoles
  • AspNetUsers

我了解如何使用 EF6 执行此操作:

I understand how to do this using EF6:

 protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<IdentityUser>()
            .ToTable("Users", "dbo").Property(p => p.Id).HasColumnName("User_Id");
        modelBuilder.Entity<User>()
            .ToTable("Users", "dbo").Property(p => p.Id).HasColumnName("User_Id");
    }

但是,由于 DbModelBuilder 已被 ModelBuilder 取代,因此我在使用 EF7 时遇到了困难.有什么建议吗?

However I'm struggling with EF7 as the DbModelBuilder has been replaced with ModelBuilder. Any suggestions?

推荐答案

必须在SqlServerPropertyBuilder中使用ForSqlServer或ForRelational更改列名,在modelBuilder中更改表名

You must use ForSqlServer or ForRelational in the SqlServerPropertyBuilder to change the column name and in the modelBuilder to change the table name

modelBuilder.Entity<IdentityUser>()
                .Property(o => o.Id)
                .ForSqlServer()
                .Column("User_Id")

 modelBuilder.Entity<IdentityUser>()
                    .ForSqlServer()
                    .Table("User")

更新:对于 beta 5 不再强制使用 ForSqlServer


Update :For the beta 5 is not longer mandatory to use ForSqlServer

这篇关于覆盖默认标识表名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 15:21