Entity Framework DbContext is initialized with wrong constructor

2

In my ASP.NET Core Web API, I add the DbContext to services:

 services.AddDbContext<OpContext>(options =>  options.UseSqlServer(Configuration["DatabaseConnectionString"]));

The problem is that my dbcontext as three constructors:

 public partial class UppContext : DbContext
 {
    public UppContext() : base()
    { }

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

    public UppContext(IIdentificationService idService) : base()
    {
        _idService = idService;
    }

And OpContext inherits from the above:

public partial class OpContext : UppContext
{
    public OpContext() : base() { }
    public OpContext(IIdentificationService idService) : base(idService) { }

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

And instead of calling the one with options, it always calls the parameterless constructor. Therefore my connection does not work.

This is where I inject it in the controller :

  public RequestController(OpContext dbContext)
    {
        cxt = dbContext;
        persistenceManager = new OpPersistenceManager(dbContext);
    }

This is the actual exception:

enter image description here

System.InvalidOperationException HResult=0x80131509 Message=No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext.
Source=Microsoft.EntityFrameworkCore StackTrace: at Microsoft.EntityFrameworkCore.Internal.DbContextServices.Initialize(IServiceProvider scopedProvider, IDbContextOptions contextOptions, DbContext context)
at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider() at Microsoft.EntityFrameworkCore.DbContext.get_ChangeTracker() at Breeze.Persistence.EFCore.EFPersistenceManager`1..ctor(T context)
at Op.Authors.Api.Breeze.OpPersistenceManager..ctor(OpContext dbContext) in C:\dev\UPP\Source\Op\Op.Authors.Api\Breeze\OpPersistenceManager.cs:line 8 at Op.Authors.Api.Controllers.RequestController..ctor(OpContext dbContext) in C:\dev\UPP\Source\Op\Op.Authors.Api\Controllers\RequestController.cs:line 21 at Microsoft.AspNetCore.Mvc.Controllers.ControllerActivatorProvider.<>c__DisplayClass4_0.b__0(ControllerContext controllerContext) at Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.<>c__DisplayClass5_0.g__CreateController|0(ControllerContext controllerContext) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()

Am I missing something?

asp.net-core
entity-framework-core
dbcontext
asked on Stack Overflow Jul 8, 2020 by Sam • edited Jul 8, 2020 by Sam

1 Answer

3

Disclaimer: I reproduced your code, but with one level of inheritance.

Changing

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

to

public OpContext(DbContextOptions<OpContext> options) : base(options)   { }  

resolved the issue for me.

Notice the type argument for DbContextOptions.

answered on Stack Overflow Jul 8, 2020 by edo.n

User contributions licensed under CC BY-SA 3.0