1

I have been following this book on how to Add Identity to an existing project (specifically section 14.4). I have done the following

  • Added the ASP.NET Core Identity NuGet packages
  • Configured Startup to use Authentication Middleware and added Identity services to the DI container
  • Updated the EF Core data model with the Identity entities

Code for updating the EF Core data model to support Identity

namespace Hoook.Data

    {
        public class HoookDbContext : IdentityDbContext<ApplicationUser>
        {
            public HoookDbContext(DbContextOptions<HoookDbContext> options) : base(options)
            {
    
            }
            public DbSet<Venture> Ventures { get; set; }
        }
    }

Code for custom user type which inherits from IdentityUser

namespace Hoook.Data

{
    public class ApplicationUser : IdentityUser
    {

    }
}

Startup.cs code for adding Identity service to DI

services.AddDefaultIdentity<ApplicationUser>(options =>
                options.SignIn.RequireConfirmedAccount = true).AddEntityFrameworkStores<HoookDbContext>();

Now I have then ran the following command in VSCode dotnet ef migrations add AddIdentitySchema
Then I ran dotnet ef database update
At this point I am to assume my database is updated and contains the Identity tables, but that is incorrect. How Am I to add the Identity tables to my existing database? I have searched through these 1, 2, 3 stackoverflow questions with no luck.
Adding a screenshot of my SSMS Migration History and tables. The migration took but no Identity tables have been populated.

enter image description here


UPDATE
It may help to note, I have created an Identity app using the .NET Core CLI with new webapp -au Indi- vidual -uld. Inside the migrations of that app after running dotnet ef database update, the Migrations up and down methods are actually populated. However in my app when adding the migration following the above steps, my Migration file looks as so:

using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Hoook.Migrations
{
    public partial class AddIdentitySchema : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {

        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {

        }
    }
}
Duke3e33
  • 151
  • 1
  • 3
  • 12
  • do commands "migrations add" and "database update" return any errors? – Timothy Sep 15 '22 at 19:11
  • No errors. running `dotnet ef migrations list` however I get the error that more than one DbContext was found. Which was not the case previously. however if I run `dotnet ef migrations list --context HoookdbContext` then the migrations are listed. I don't know if that information helps the cause – Duke3e33 Sep 15 '22 at 20:53
  • You can ignore the above about the error on the `dotnet ef migrations list` issue. It seems when I was trying to figure this post's issue out I ran a `dotnet ef dbcontext scaffold` command and it generated another context file which I have now deleted. – Duke3e33 Sep 15 '22 at 21:42
  • Try command `dotnet ef migrations add AddIdentitySchema --context HoookdbContext` as it's said in https://learn.microsoft.com/en-us/ef/core/cli/dotnet#common-options – Timothy Sep 15 '22 at 21:57
  • If I look at my Microsoft SQL Server Management studio under my Migrations history, the migration to `AddIdentitySchema` is there. Just the tables are not populated. I can add a screenshot of the tables and migration list above as well if that will help? – Duke3e33 Sep 15 '22 at 22:34
  • Your migration file is empty, it should not be empty. In your `HoookDbContext ` there should be a method called `OnModelCreating` make sure you are calling the base method in it : ` protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); //.... REST OF YOUR LOGIC } ` – Dawood Awan Sep 16 '22 at 07:46
  • Once that is sorted, then try add the migration again - this time the migration file should not be empty. – Dawood Awan Sep 16 '22 at 07:48
  • From the migration file you shared, it seems that you did not migrate successfully. – Chen Sep 16 '22 at 08:09
  • @DawoodAwan so I would have to use Fluent API to configure my model. Calling `builder.Entity.ToTable("Some name")` for each [entity](https://learn.microsoft.com/en-us/aspnet/core/security/authentication/customize-identity-model?view=aspnetcore-6.0) type in Identity. Then also create the corresponding class to that entity? – Duke3e33 Sep 16 '22 at 11:21
  • Not exactly, all Identity related Entities are already included in the `IdentityDbContext` class. You can see the source code here: `https://github.com/aspnet/AspNetIdentity/blob/main/src/Microsoft.AspNet.Identity.EntityFramework/IdentityDbContext.cs` But if you are not calling the `base.OnModelCreating()` in your own `OnModelCreating` EF will not be able to find the ones from IdentityDbContext. How are you currently configuring your entities? Do you have OnModelCreating in `HoookDbContext `? – Dawood Awan Sep 16 '22 at 11:38
  • 1
    I see. So I just need to override `OnModelCreating` and call `base.OnModelCreating()` to have the entities added from the `IdentityDbContext` to the model. However, I also have the ability to change the default table names and generic types from Identity. But the most important step is calling `base.OnModelCreating()` to be able to have the entities added to my database – Duke3e33 Sep 16 '22 at 12:10

1 Answers1

1

Thanks to the comments below and @DawoodAwan, I have figured out the missing piece. I did not override the OnModelCreating method and subsequently call the base.OnModelCreating() method inside of it. The solution is in the below code. I have also gone ahead and updated the names of the default tables in this example:

using Hoook.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace Hoook.Data
{
    public class HoookDbContext : IdentityDbContext<ApplicationUser>
    {
        public HoookDbContext(DbContextOptions<HoookDbContext> options) : base(options)
        {

        }
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            builder.Entity<ApplicationUser>().ToTable("Users");
            builder.Entity<IdentityRole>().ToTable("Roles");
            builder.Entity<IdentityUserClaim<string>>().ToTable("Claims");
            builder.Entity<IdentityUserToken<string>>().ToTable("Tokens");
            builder.Entity<IdentityUserLogin<string>>().ToTable("Logins");
            builder.Entity<IdentityRoleClaim<string>>().ToTable("RoleClaims");
            builder.Entity<IdentityUserRole<string>>().ToTable("UserRoles");
        }
        public DbSet<Venture> Ventures { get; set; }
    }
}
Duke3e33
  • 151
  • 1
  • 3
  • 12