简体   繁体   中英

How to add a column in an existing AspNetUsers table

I need to add several columns to the 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:

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. But I don't see it anywhere in my blazor application.

在此输入图像描述

Could you please help me with this? Thank you.

Steps:

  • add the properties to the ApplicationUser class.
  • make sure that class inherits from IdentityUser
  • find all other occurences of IdentityUser in your .cs and .razor files and replace with ApplicationUser .

  • inherit the context with the new class as Type argument:
    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>

  • when you override OnModelCreating, don't forget to call
    base.OnModelCreating(builder);

  • now you can add-migrate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM