【问题标题】:How to add a column in an existing AspNetUsers table如何在现有 AspNetUsers 表中添加列
【发布时间】:2019-08-17 12:27:51
【问题描述】:

我需要向 dbo.AspNetUsers 添加几列。这是使用“个人帐户”选项创建的。我已经尝试了我在互联网上搜索的内容,但我无法让它工作。

在生成的 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 应用程序中的任何地方都没有看到它。

我可以尝试什么来解决这个问题?

【问题讨论】:

  • 这是实际代码吗,即您已将属性添加到 DbContext ?向我们展示ApplicationUser 类。
  • @Henk Hollerman 就是这样。如果你会看图像。我没有看到任何应用程序用户。但在我在网上进行的搜索中,我应该将新属性放在那个 ApplicationUser 类上。
  • 我可以在那里看到一个 ApplicationUser.cs 文件。仔细看。 VS 在解决方案资源管理器中也有一个搜索框。

标签: c# entity-framework blazor


【解决方案1】:

步骤:

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

  • 使用新类作为类型参数继承上下文:
    public class ApplicationDbContext : IdentityDbContext&lt;ApplicationUser&gt;

  • 当您覆盖 OnModelCreating 时,别忘了致电
    base.OnModelCreating(builder);

  • 现在您可以添加-迁移

【讨论】:

    猜你喜欢
    • 2017-10-04
    • 1970-01-01
    • 2014-06-28
    • 2015-10-26
    • 2020-02-21
    • 2022-01-01
    • 2018-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多