Unable to register DbConnection with Unity and Entity Framework

不羁的心 提交于 2020-01-25 01:07:10

问题


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:


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"));


来源:https://stackoverflow.com/questions/35974965/unable-to-register-dbconnection-with-unity-and-entity-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!