简体   繁体   English

如何在现有AspNetUsers表中添加列

[英]How to add a column in an existing AspNetUsers table

I need to add several columns to the dbo.AspNetUsers. 我需要在dbo.AspNetUsers中添加几个列。 This has been created using the "Individual Accounts" option. 这是使用“个人帐户”选项创建的。 I have tried what I have searched on the internet but I can't get it to work. 我已经尝试过在互联网上搜索过的内容,但我无法让它工作。 Please see my code: 请看我的代码:

In the generated ApplicationDbContext I modified it to be like so: 在生成的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)
    {
    }
}

Then I ran the add-migration but I just got an empty Migration file. 然后我运行了添加迁移,但我得到了一个空的迁移文件。

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)
        {

        }
    }
}

But I still tried the update-database but nothing has been added to the table that I needed. 但我仍然尝试了更新数据库,但没有添加到我需要的表中。

I think I need to place it in the ApplicationUser that inherits IdentityUser. 我想我需要将它放在继承IdentityUser的ApplicationUser中。 But I don't see it anywhere in my blazor application. 但我在我的blazor应用程序中没有看到它。

在此输入图像描述

Could you please help me with this? 你能帮帮我吗? Thank you. 谢谢。

Steps: 脚步:

  • add the properties to the ApplicationUser class. 将属性添加到ApplicationUser类。
  • make sure that class inherits from IdentityUser 确保该类继承自IdentityUser
  • find all other occurences of IdentityUser in your .cs and .razor files and replace with ApplicationUser . 在.cs和.razor文件中找到IdentityUser所有其他出现,并替换为ApplicationUser

  • inherit the context with the new class as Type argument: 使用新类作为Type参数继承上下文:
    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>

  • when you override OnModelCreating, don't forget to call 当你重写OnModelCreating时,不要忘记打电话
    base.OnModelCreating(builder);

  • now you can add-migrate 现在你可以添加 - 迁移

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从WebAPI更新AspNetUsers表中现有用户记录的密码 - How to update password for the existing user record in AspNetUsers table from WebAPI 使用ASP.Net Identity EntityFramwork到ID列设置为UniqueIdentifier的现有AspNetUsers表 - Using ASP.Net Identity EntityFramwork to Existing AspNetUsers table with ID Column set to UniqueIdentifier 如何在AspNetUsers表中保存字段 - How to save a field in the AspNetUsers table 我想在另一个表中添加 AspNetUsers 表“Id”列作为外键 - I want to add AspNetUsers table "Id" column as Foreign Key in another table 如何自定义 AspNetUsers 表的表名 - how to customize the table name for the AspNetUsers table 如何正确地从使用Package Manager控制台进行迁移期间创建的AspNetUsers表中删除列? - How to properly remove a column from AspNetUsers table that was created during a migration with Package Manager Console? 实体框架将实体对象映射到现有表AspNetUsers - Entity Framework map entity object to existing table AspNetUsers ASP.NET MVC-将Identity与现有的经过修改的AspNetUsers表一起使用 - ASP.NET MVC - Using Identity with existing, modified AspNetUsers table 如何向User.Identity添加另一个Propertys来自身份2.2.1中的表AspNetUsers - How to Add another Propertys to User.Identity From table AspNetUsers in identity 2.2.1 如何将 ADFS 用户添加到 Identity 中的“AspNetUsers”? - How to add an ADFS user to `AspNetUsers` in Identity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM