【问题标题】:Is there a simple way to add a column in the AspNetRoles table?有没有一种简单的方法可以在 AspNetRoles 表中添加一列?
【发布时间】:2019-10-11 22:07:45
【问题描述】:

AspNetRoles 表有 2 列“Id”和“Name”。我需要添加第三个:“模块(nvarchar(256),null)”。

我在搜索中发现了一些文章,但大部分是几年前的文章,对我来说有点复杂。我在问是否有使用 EF 的简单方法?

【问题讨论】:

  • 有什么理由不能只在 SQL 中完成?
  • 据我所知,如果你这样做,该列将在 EF 代码中不可见

标签: c# visual-studio entity-framework


【解决方案1】:

您可以创建一个继承自 IdentityRole 的自定义类,并在该类中添加您想要的任何属性:

  1. 创建一个自定义类,如下图:

     public class CustomIdentityRole : IdentityRole
        {
            public string NewColumn { get; set; }
    
        }
  2. 运行EF迁移命令生成表模型如下图:

Add-Migration test2

public partial class test2 : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<string>(
                name: "NewColumn",
                table: "AspNetRoles",
                nullable: true);
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
          
            migrationBuilder.DropColumn(
                name: "NewColumn",
                table: "AspNetRoles");
        }
    }
  1. 运行 EF 更新以添加新列:

Update-Database

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-07
    • 2021-02-19
    • 2019-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-18
    • 2011-03-08
    相关资源
    最近更新 更多