无法在Unity和Entity Framework中注册DbConnection [英] Unable to register DbConnection with Unity and Entity Framework

查看:92
本文介绍了无法在Unity和Entity Framework中注册DbConnection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完全不确定导致此异常的根本问题是什么。

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

我正在将ASP.NET MVC与Unity.Mvc和Entity一起使用框架6.我有以下代码来注册我的存储库:

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();
    }
}

存储库具有以下代码:

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>();
        }
    }

我遇到的问题是,如果我在 RegisterTypes方法中有一个断点,我可以看到容器肯定正在注册所有存储库,但是在存储库的构造函数中的断点永远不会被命中。

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.

所以我认为断点没有被击中,并且我没有注册 System.Data.Common.DbConnection这一事实意味着 DbContext

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.

我找不到有关如何在实体框架的Unity和DbContext中使用 System.Data.Common.DbConnection的任何有用信息。 。

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

我该如何解决?

推荐答案

您应该添加您的 RegisterTypes 如何构建 DbContext ,以及可能的生命周期。

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

如果您有自己的类(例如 CustomContext ),则继承了 DbCo ntext ,进行注册。假定您的默认生存期足够:

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

container.RegisterType<DBContext, CustomContext>();

如果直接使用 DbContext ,请指示Unity它应该使用哪个构造函数。例如,假设您的连接字符串名为 appConnectionString

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

这篇关于无法在Unity和Entity Framework中注册DbConnection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆