Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Register a DB Context with Simple Injector and .Net Core

Hi I'm using Simple Injector for my application DI. I have kept the default DI for framework DI.

But I need to register the DbContext with SimpleInjector. When I run the application

container.Verify()

Gives the following error

ActivationException: The constructor of type UsersDbContext contains the parameter with name 'options' and type DbContextOptions that is not registered. Please ensure DbContextOptions is registered, or change the constructor of UsersDbContext.

I'm registering the DbContext with SimpleInjectore in a function SimpleInjectorConfig.InitializeContainer(app, _container)

// DbContext
container.Register<DbContext, UsersDbContext>();

And my Startup is

public void ConfigureServices(IServiceCollection services)
{
    var conString = Configuration.GetConnectionString("DefaultConnection");

    // Add framework services.
    services.AddDbContext<UsersDbContext>(options => options.UseSqlServer(conString));

    services.AddIdentity<User, IdentityRole>()
            .AddEntityFrameworkStores<UsersDbContext>()
            .AddDefaultTokenProviders();

    IdentityConfigurationService.ConfigureIdentityOptions(services);

    services.AddMvc();

    // Add application services
    services.AddSingleton<IControllerActivator>(new SimpleInjectorControllerActivator(_container));
    services.AddSingleton<IViewComponentActivator>(new SimpleInjectorViewComponentActivator(_container));

    services.UseSimpleInjectorAspNetRequestScoping(_container);
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    SimpleInjectorConfig.InitializeContainer(app, _container);

    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();

    _container.Verify();

    app.UseMvc();
}

I know the problem is with the options, but I don't know how Simple Injector needs the default Connection string registered to the container. Is this something that can be passed to Simple Injector ? or should I use the DbContextFactory to pass the connection string to UserDbContext ?

like image 948
Jeff Finn Avatar asked Apr 24 '17 21:04

Jeff Finn


People also ask

How do I create a database context in .NET core?

To have a usable Entity Framework DBContext, we need to change the configuration of the application. We will need to add a connection string so that our DBContext knows which server to go to and which database to query. We will put the connection string in a JSON configuration file.

What is DB context .NET core?

A DbContext instance represents a session with the database and can be used to query and save instances of your entities. DbContext is a combination of the Unit Of Work and Repository patterns. Entity Framework Core does not support multiple parallel operations being run on the same DbContext instance.

How do I create a DbContext option?

If you really want to create the context manually, then you can configure it like this: var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>(); optionsBuilder. UseSqlServer(Configuration. GetConnectionStringSecureValue("DefaultConnection")); _context = new ApplicationDbContext(optionsBuilder.


2 Answers

You need to tell SimpleInjector how to instantiate the UsersDbContext which seems to have a constructor with the parameter of type DbContextOptions.

Change how you register your DbContext by using an overload of Register method that take a delegate (factory) parameter like below:

container.Register<DbContext>(() => {
    var options = // Configure your DbContextOptions here
    return new UsersDbContext(options);
});
like image 135
CodeNotFound Avatar answered Oct 20 '22 09:10

CodeNotFound


All answers don't work for me, but I found another approach.

container.CrossWire<IntellisenseDbContext>(app);

It works

like image 1
Alados Avatar answered Oct 20 '22 08:10

Alados