c# - 如何在现有的 AspNetUsers 表中添加一列

标签 c# entity-framework blazor

我需要向 dbo.AspNetUsers 添加几列。这是使用“个人帐户”选项创建的。我尝试了我在 Internet 上搜索到的内容,但无法正常工作。

在生成的 ApplicationDbContext 中,我将其修改为如下所示:

public class ApplicationDbContext : IdentityDbContext
{
    [Required]
    [MaxLength(50)]
    public string FirstName { get; set; }
    [MaxLength(50)]
    public string MiddleName { get; set; }
    [Required]
    [MaxLength(50)]
    public string LastName { get; set; }

    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }
}

然后我运行了 add-migration,但我得到的只是一个空的迁移文件。

using Microsoft.EntityFrameworkCore.Migrations;

namespace BlazorApp4.Data.Migrations
{
    public partial class ModifiedUserDatabase : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {

        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {

        }
    }
}

但我仍然尝试更新数据库,但没有向表中添加任何我需要的内容。

我觉得需要放在继承IdentityUser的ApplicationUser中。但我在我的 Blazor 应用程序中没有看到它。

enter image description here

我可以尝试如何解决这个问题?

最佳答案

步骤:

  • 将属性添加到 ApplicationUser 类。
  • 确保该类继承自 IdentityUser
  • 找到所有其他出现的 IdentityUser在您的 .cs 和 .razor 文件中替换为 ApplicationUser .

  • 使用新类作为类型参数继承上下文:
    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>

  • 当您覆盖 OnModelCreating 时,不要忘记调用
    base.OnModelCreating(builder);

  • 现在您可以添加-迁移

关于c# - 如何在现有的 AspNetUsers 表中添加一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57536271/

相关文章:

c# - 在 C# 中读取 Excel 文件

c# - 在 WebClient.DownloadFile 期间如何解决 c# 中的路径太长异常

c# - 理解枚举——用作常量

c# - 如何在 Blazor 中设置输入数字的格式

asp.net-core - 如何重定向到主页?

c# - Row_Number() 在 Nhibernate 中使用 CreateCriteria 进行分区

c# - 如何制作一个适用于多种形式的变量?

c# - Code First Entity Framework - 更改连接字符串

c# - 使用附加子句更新实体

asp.net-core - 服务器端 blazor 中的 session 超时重定向