使用 Entity Framework 代码优先迁移更改数据库 table 名称

Change a database table name using Entity Framework code first migrations

您将如何使用实体框架代码优先迁移更改 table 的名称。

下面的代码将我的 table 创建为:

RoleBindingModel、DivisionBindingModel 等,这并不理想。

如何将名称更改为自定义 table 名称? (不更改您的模型名称)

IdentityModels.cs

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
        }

        public DbSet<RoleBindingModel> Role { get; set; }
        public DbSet<DivisionBindingModel> Division { get; set; }
        public DbSet<CategoryBindingModel> Category { get; set; }
        public DbSet<ProductBindingModel> Product { get; set; }


        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
    }

一种方法是使用 DataAnnotations 和 Table 属性。

using System.ComponentModel.DataAnnotations.Schema;    

[Table("Role")]
public class RoleBindingModel
{

}