Code Monkey home page Code Monkey logo

Comments (4)

ellern avatar ellern commented on July 17, 2024 2

@BloodBaz the reason it's not working, is probably because you didn't register a IDbContextFactory. Inside DbContextCollection.cs it checks if the factory is registered with the following code:

 var dbContext = _dbContextFactory != null 
   ? _dbContextFactory.CreateDbContext<TDbContext>()
   : Activator.CreateInstance<TDbContext>();

If it's not registered it will use the Activator.CreateInstance, but instead you want it to use your DI.

Whenever I need to use one of my DbContext I do the following:

public async Task<Foo> GetFooAsync(Guid id)
{
    using (var dbContextScope = _dbContextScopeFactory.CreateReadOnly())
    {
        return await dbContextScope.DbContexts.Get<FooDbContext>()
            .Foo
            .FirstOrDefaultAsync(q => q.Id == id);
    }
}

I'm using Simple Injector for my DI, here is the setup.

container.Register<IDbContextScopeFactory>(() => new DbContextScopeFactory(new SimpleInjectorDbContextFactory(container)), Lifestyle.Scoped);
internal class SimpleInjectorDbContextFactory : IDbContextFactory
{
    readonly Container _container;

    public SimpleInjectorDbContextFactory(Container container)
    {
        _container = container;
    }

    public TDbContext CreateDbContext<TDbContext>() where TDbContext : DbContext
    {
        return _container.GetInstance<TDbContext>();
    }
}

from dbcontextscope.

BloodBaz avatar BloodBaz commented on July 17, 2024

I have a similar problem. My connection string is a function of a named app.config connection string AND another parameter which I have my own interface/implementation that provides this (IConnectionStringResolver). I want to inject it as follows but AmbientDbContextLocator requires a parameterless constructor for my DbContext-derived class:

public class TaskManagementDbContext : DbContext
{
    public TaskManagementDbContext(IConnectionStringResolver csr) :
        base(csr.GetConnectionString("Default"))
    {
    }
}

This is fine when I use NInject to instantiate the DbContext directly but when trying to use with DbContextScope, it is AmbientDbContextLocator that is doing the instantiation and it throws a MissingMethodException because it requires my DBContext-derived class to have a parameterless constructor:

public class TaskRepository : ITaskRepository
{
    private readonly IAmbientDbContextLocator _ambientDbContextLocator;

    private TaskManagementDbContext DbContext
    {
        get
        {
            // MissingMethodException thrown "No parameterless constructor defined for this object"
            var dbContext = _ambientDbContextLocator.Get<TaskManagementDbContext>();
            ...
        }
    }

Is there a work-around for this? Thanks!

from dbcontextscope.

mitsbits avatar mitsbits commented on July 17, 2024

Here's a DbContextFactory that works wiith DbContextScope just fine. Connection string form config.
https://github.com/mitsbits/Ubik/blob/master/Ubik.EF/DbContextFactory.cs

from dbcontextscope.

BloodBaz avatar BloodBaz commented on July 17, 2024

@ellern Thanks for the pointers. This is very useful.
@mitsbits Thanks for the example.

from dbcontextscope.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.