Ibanez1408
Ibanez1408

Reputation: 5058

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.

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.

enter image description here

What can I try to resolve this?

Upvotes: 3

Views: 975

Answers (1)

Henk Holterman
Henk Holterman

Reputation: 273199

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

Upvotes: 2

Related Questions