Duke3e33
Duke3e33

Reputation: 321

Integrating ASP.NET Core Identity db with an already created database in a Razor Pages app

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

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

        }
    }
}

Upvotes: 2

Views: 1705

Answers (1)

Duke3e33
Duke3e33

Reputation: 321

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; }
    }
}

Upvotes: 2

Related Questions