Search code examples
asp.net-core-mvc.net-6.0

.NET 6 - Create Identity Context on Existing Database


I am new to .NET and learning how to create Identity and Login pages. I have the database context Database1DbContext for testing and it has a couple tables already.

I followed tutorials to add builder.Services.AddIdentity, IdentityUser, IdentityRole... in the Programs.cs. The DbContext requires to inherit from IdentityDbContext and I am not sure how to re-use the existing public class Database1DbContext : DbContext, so I ended up create another

public class IdentityDatabase1DbContext : IdentityDbContext

which is pointing to the same database connection (see below code).

Here is the code in the DbContext.cs file:

public class Database1DbContext : DbContext
{
    public Database1DbContext(DbContextOptions<Database1DbContext> options) : base(options)
    {
    }

    public DbSet<TransactionModel> Transactions { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    }
}

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

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
    }
}

This code is in the Program.cs file:

builder.Services.AddDbContext<Database1DbContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("ConnDatabase1")));

builder.Services.AddDbContext<IdentityDatabase1DbContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("ConnDatabase1")));
    
    
// For Identity
builder.Services.AddIdentity<IdentityUser, IdentityRole>()
    .AddEntityFrameworkStores<IdentityDatabase1DbContext>()
    .AddDefaultTokenProviders();

// Adding Authentication
builder.Services.AddAuthentication(options =>
{
    options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
    options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
});

I did the add-migration, update-database and it created AspNetUsers, AspNetRoles... tables and everything works fine.

However, the way I did seem duplicated/inefficient because I created duplicate context and entries multiple places.

Is there a way to make Database1DbContext inherit both DbContext and IdentityDbContext or somehow make it more efficient?

Thanks in advance.


Solution

  • Your code in DbContext.cs will be-

    public class Database1DbContext: IdentityDbContext<IdentityUser>
    {
        public Database1DbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
        {
    
        }
    
        public DbSet<TransactionModel> Transactions { get; set; }
    
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
        }
    }
    

    and the code in is in the Program.cs file will be-

    // Identity
    builder.Services.AddIdentity<IdentityUser, IdentityRole>().AddEntityFrameworkStores<Database1DbContext>().AddDefaultTokenProviders();