2

I am not at all sure what the underlying problem is that is causing this exception.

I am using ASP.NET MVC, with Unity.Mvc, and Entity Framework 6. I have the following code to register my repositories:

    public static void RegisterTypes(IUnityContainer container)
    {
        // NOTE: To load from web.config uncomment the line below. Make sure to add a Microsoft.Practices.Unity.Configuration to the using statements.
        // container.LoadConfiguration();

        // TODO: Register your types here
        // container.RegisterType<IProductRepository, ProductRepository>();

        container.RegisterType<IGenericRepository<Customer>, GenericRepository<Customer>>();
        container.RegisterType<IGenericRepository<Product>, GenericRepository<Product>>();
        container.RegisterType<IGenericRepository<Order>, GenericRepository<Order>>();
        container.RegisterType<IGenericRepository<OrderItem>, GenericRepository<OrderItem>>();
        container.RegisterType<IGenericRepository<Supplier>, GenericRepository<Supplier>>();
    }

And then in a controller I have:

public class IndexController : Controller
{
    public IndexController(IGenericRepository<Customer> testGenericRepository)
    {
        var result = testGenericRepository.SelectAll();
    }

    public ActionResult Index()
    {
        return View();
    }
}

And the repository has the following code:

public class GenericRepository<T> : IGenericRepository<T>
        where T : class
    {
        private readonly DbContext _dbContext;
        private readonly IDbSet<T> _dbSet;

        public GenericRepository(DbContext dbContext)
        {
            if (dbContext == null)
            {
                throw new ArgumentNullException(nameof(dbContext));
            }

            _dbContext = dbContext;
            _dbSet = _dbContext.Set<T>();
        }

        public IEnumerable<T> SelectAll()
        {
            return _dbSet.AsEnumerable<T>();
        }
    }

The problem that I'm having is that if I have a breakpoint in the "RegisterTypes" method, I can see that the container is definitely getting all the repositories registered, but a breakpoint in the constructor of the repositories never gets hit.

So I think that the fact that the breakpoint does not get hit, and I have not registered a "System.Data.Common.DbConnection" means that the DbContext that the repository uses never gets set.

I can't find any useful information about how to use "System.Data.Common.DbConnection" with Unity and the DbContext from Entity Framework.

How do I resolve this?

1 Answer 1

3

You should add to your RegisterTypes how to build your DbContext, and probably with which lifetime.

If you have your own class (say CustomContext) inheriting from DbContext, register it. Supposing your default lifetime is adequate:

container.RegisterType<DBContext, CustomContext>();

If you use directly DbContext, instruct Unity which constructor it should use. By example, supposing your connection string is named appConnectionString:

container.RegisterType<DBContext>(
    new InjectionConstructor("name=appConnectionString"));
Sign up to request clarification or add additional context in comments.

2 Comments

That was it! Thank you. I am surprised this was not mentioned more in the documentation.
Well, when registering a type, you should check its dependencies and ensure they are registered too. Otherwise Unity will try what it can to resolve those dependencies and will likely fail.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.