0

I am working on an ASP.NET core project with several DbContexts, one of them is a Identity Context.

I applied Tao Zhou's (Aspnet core Identity custom ApiAuthorizationDbContext) solution to a problem and this solved my problem first, but now I have the problem that I can't create an ApplicationDbContext without parameters.

  • DbContextOptions options
  • IOptions operationalStoreOptions

Here are a few coding lines to illustrate it

I have in the Startup the ApplicationDbContext registered

Startup

services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(
            Configuration.GetConnectionString("IdentityConnection"))
       );

ApplicationDbContext

    public class ApplicationDbContext : KeyApiAuthorizationDbContext<Identity.AspNetUsers, Identity.AspNetRoles, Guid>

    public ApplicationDbContext(DbContextOptions options,
                        IOptions<OperationalStoreOptions> operationalStoreOptions) : base(options, operationalStoreOptions)
{

}

If I want to create a new

ApplicationDbContext ctx = new ApplicationDbContext();

I need these two parameters...

It'd be great if someone could give me a hint.

Thank you and Best Regards, Noel

Noel
  • 1
  • 1
    Welcome to StackOverflow Noel. Can you explain why you would want to create a context like that instead of using Dependency Injection to access the context? – Dennis VW Dec 15 '19 at 09:59
  • Hello Dennis1769, thank you for you answer. I have a complex application architecture and i want to loose encapsulation of EF Core and Controller / BusinessLogic. For other Things I using DI. – Noel Dec 16 '19 at 08:14

1 Answers1

0

Try to modify the context class like below example:

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

    }

    // C# will call base class parameterless constructor by default
    public ApplicationDbContext()
    { }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        IConfigurationRoot configuration = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json")
        .Build();
        optionsBuilder.UseSqlServer(configuration.GetConnectionString("DefaultConnection"));
    }
}

Reference :

https://learn.microsoft.com/en-us/ef/core/miscellaneous/cli/dbcontext-creation#using-a-constructor-with-no-parameters

https://learn.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext#onconfiguring

Xueli Chen
  • 11,987
  • 3
  • 25
  • 36
  • Thank you Xueli Chen, but I think this will be not solve my problem. I need the two parameters for the base class "KeyApiAuthorizationDbContext". – Noel Dec 17 '19 at 10:14
  • This is just my test demo .Is your demand to instantiate a DbContext without parameters? Did you try this in the `ApplicationDbContext` inherited from `KeyApiAuthorizationDbContext` ? – Xueli Chen Dec 18 '19 at 01:47