0

I am eventually trying to create a library to help me implement Identity into a .NET 6 webapi. I am trying to separate all my code for auth into it's own files so I can eventually replace with my custom library. I want to shied my app from the ApiUser, if I can. I have an issue now where I am getting the error "More than one DbContext was found. Specify which one to use...."

I am not sure how to extend the IdentityDbContext so that I can inject some tables/data of my own without needing to specify which context to use. Technically, my app only uses the one context "ApiDbContext".

ApiAuthIdentityContext.cs - this hides ApiUser and injects seed data

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


    public class ApiDbContext : ApiAuthDbContext
    {
        public ApiDbContext(DbContextOptions<ApiDbContext> options) : base(options)
        {
        }
        
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            //seed data for auth
            modelBuilder.ApplyConfiguration(new SeedDataRoles());


        }

Program.cs

builder.Services.AddDbContext<ApiDbContext>(options =>
               options.UseNpgsql(builder.Configuration["DbConnection"])
            );

Is there another way to do this where the app only sees the ApiDbContext?

a2ron44
  • 1,711
  • 1
  • 13
  • 18
  • I know I can just use --context when migrating, but I don't see why I should have to. – a2ron44 Jan 20 '23 at 16:36
  • I'm not sure why I can extend DbContext, but not this new class I just created? – a2ron44 Jan 23 '23 at 18:53
  • There's a previous post that addresses this https://stackoverflow.com/questions/52311053/more-than-one-dbcontext-was-found. As long as there are multiple classes inheriting from DbContext in the same assembly you'll be prompted with this message. Once you extract `ApiAuthDbContext` into its own library you'll probably stop seeing this message. – Nicholas Bergesen Jan 27 '23 at 17:42

0 Answers0