Asp.Net Core 2.1 add-migration is not picking up properties in class that inherits from IdentityUser

asp.net-core-2.0 asp.net-core-2.1 asp.net-identity ef-core-2.1 entity-framework-core

Question

I am working on an ASP.Net Core 2.1.1 web application in VS2017 v15.8.2, using the web application template with individual user accounts.

I want to add more properties to the IdentityUser so I created an ApplicationUser class that inherits from IdentityUser. I applied the appropriate updates to the startup.cs and _loginPartial.cshtml. Everything compiles, but when I run Add-Migration it doesn't pick up the properties in my ApplicationUser class. I have a migration class but the up and down methods have no properties.

I only have one project so selecting the Default Project is not the issue in Package Manager Console.

Here is my ApplicationUser class

public class ApplicationUser : IdentityUser
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Street { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zipcode { get; set; }
    public int CompanyId { get; set; }
}

Here is my DBContext

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

Here is the service in Startup.cs, ConfigureService

        services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(Configuration.
        GetConnectionString("AccountDbConnectionString")));

        services.AddDefaultIdentity<ApplicationUser>()
            .AddEntityFrameworkStores<ApplicationDbContext>();

Here is the add-migration in Package Manager Console

Add-Migration -c ApplicationDbContext V1.00.01 

Here is the resultant migration

public partial class V10001 : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {

    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {

    }
}

The ApplicationUser class is in the Models folder off the project root.

The IdentityUser was parsed for the initial Asp.Net Core Web Application template migration when I first created the project and when I ran the update-database command, it created all of the AspNetUserxxx and AspNetRolexxx tables in the database just fine.

But I cannot see why EF Migrations is not seeing the ApplicationUser class so it can add the new properties to the migration class.

When I look for examples online, it looks like I am doing everything correctly to extend the IdentyUser properties.

What am I missing?

EDIT 1: I tried the solution in this SO post but it made no difference. Entity Framework Core 2.0 add-migration not generating anything

1
0
8/31/2018 3:55:10 PM

Popular Answer

You need to add the ApplicationUser class in DBContext.

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

    public virtual DbSet<ApplicationUser> ApplicationUser { get; set; }
}

Then Add migration and then Update database.

2
8/31/2018 8:35:03 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow