MVC 5 with Ninject and EF Core, Context throws exception after one query

.net-4.6.1 asp.net-mvc-5 c# entity-framework-core ninject

Question

I am trying to use EF Core in our ASP.Net MVC 5 web application using Framework 4.6.1. We are using Ninject for dependency injection. I am injecting the Context in the DAL layer. I am successful a performing a single query but the context throws the following exception when the next query is run

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.

I have seen this error before but I am not really sure how it applies in my situation. What is it about my setup that is causing this error to be thrown? Here is some code:

The Controller:

public ActionResult GetEnrollmentWorkflows()
{
    var class1Entities = this._class1Service.GetAll().ToList();
    var class1EntityIds = class1Entities.Select(x => x.Class1EntityId).ToList(); // Works
    var class2Entity =
        this._class2EntityService
            .GetByClass1EntityIds(class1EntityIds).ToList(); // Fails with exception
    return PartialView("_somePartial", model);
}

The context binding:

kernel.Bind<Context.ContextTenant>().ToSelf().InTransientScope()
    .WithConstructorArgument("options",
        new DbContextOptionsBuilder<Tables.ContextTenant>()
    .UseSqlServer(ConfigurationManager.ConnectionStrings
        [EnvironmentConsts.DbConnectionTenant].ConnectionString)
    .Options);

The context:

public ContextTenant(DbContextOptions<Tables.ContextTenant> options) : base(options)
{}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    base.OnConfiguring(optionsBuilder);
}

The Ninject Modules:

public class DalModule : NinjectModule
{
    public override void Load()
    {
        this.Kernel.Bind(x =>
            x.FromThisAssembly().SelectAllClasses().Join
                .FromAssemblyContaining<ISomeInterface>()
                .SelectAllInterfaces()
                .BindAllInterfaces().Configure(y => y.InTransientScope()));
    }
}



public class ServiceModule : NinjectModule
{
    public override void Load()
    {
        this.Kernel?.Load(new[] { new DalModule() });
        this.Kernel.Bind(x =>
            x.FromThisAssembly().SelectAllClasses().Join
                .FromAssemblyContaining<ISomeInterface>()
                .SelectAllInterfaces()
                .BindAllInterfaces().Configure(y => y.InTransientScope()));
    }
}

The DAL Classes

Class1:

public class Class1Repository : IClass1Repository
{
    private ContextTenant ContextTenant { get; }

    public Class1Repository(ContextTenant contextTenant)
    {
        ContextTenant = contextTenant;
    }

    public IEnumerable<Class1Entity> GetAll()
    {
        return this.ContextTenant.Class1Entity.Select(x => x);
    }
}

Class 2:

public class Class2Repository : IClass2Repository
{
    private readonly ContextTenant _contextTenant;

    public Class2Repository(ContextTenant contextTenant)
    {
        _contextTenant = contextTenant;
    }

    public IEnumerable<Class2Entity> GetByClass1EntityIds(
        IEnumerable<int> class1EntityIds)
    {
        return this._contextTenant.Class2Entity.Where(x =>
            class1EntityIds.Contains(x.Class1EntityId));
    }
}

The Services:

public class Class1Service : IClass1Service
{
    private readonly IClass1Repository _class1Repository;

    public Class1Service(IClass1Repository class1Repository)
    {
        _class1Repository = class1Repository;
    }

    public IEnumerable<Class1Entity> GetAll()
    {
        return this._class1Repository.GetAll();
    }
}

public class Class2Service : IClass2Service
{
    private readonly IClass2Repository _class2Repository;

    public Class2Service(IClass2Repository class2Repository)
    {
        _class2Repository = class2Repository;
    }

    public IEnumerable<Class2Entity> GetByClass1EntityIds(
        IEnumerable<int> class1EntityIds)
    {
        return this._class2Repository.GetByClass1EntityIds(class1EntityIds);
    }
}

Edit: Here is a little bit more code from the start up in case this would affect anything:

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(NinjectWebCommon), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethod(typeof(NinjectWebCommon), "Stop")]
namespace MyApp
{
    public static class NinjectWebCommon
    {
        private static readonly Bootstrapper bootstrapper = new Bootstrapper();

        public static void Start()
        {
            DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
            DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
            bootstrapper.Initialize(CreateKernel);
        }

        public static void Stop()
        {
            bootstrapper.ShutDown();
        }

        private static IKernel CreateKernel()
        {
            // Bind the context
        }
    }
}
1
1
8/31/2018 1:42:43 PM

Popular Answer

I had similar problems in the past that sometimes could be solved by changing

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    base.OnConfiguring(optionsBuilder);
}

In

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    if (optionsBuilder.IsConfigured == false)
    { 
        base.OnConfiguring(optionsBuilder);
    }
}
0
9/6/2018 4:23:50 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow