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

Time:09-19

I have been following 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)
        {

        }
    }
}

CodePudding user response:

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