'的DbContext'必须是具有公共无参数构造函数

时间:2016-01-01 22:49:54

标签: c# asp.net-mvc asp.net-mvc-4

ASP.NET MVC中的新手,尝试按this tutorial on generic repositories来写信。

Bootstrapper(Unity)

public class Bootstrapper
    {
        public static IUnityContainer Initialise()
        {
            var container = BuildUnityContainer();
            DependencyResolver.SetResolver(new UnityDependencyResolver(container));
            return container;
        }
        private static IUnityContainer BuildUnityContainer()
        {
            var container = new UnityContainer();
            RegisterTypes(container);
            return container;
        }
        public static void RegisterTypes(IUnityContainer container)
        {
            // ERROR OCCURS HERE
            container.RegisterType<IRepository<DbSet>, Repository<DbContext, DbSet>>("DataAccessService"); 

            container.RegisterType<AccountController>(new InjectionConstructor());
            container.RegisterType<ManageController>(new InjectionConstructor());

        }
    }

堆栈跟踪

'DbContext' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'C' in the generic type or method 'Repository<C, T>'    

存储库界面

public interface IRepository<T> : IDisposable
        where T : class
    { 
        IQueryable<T> All();
        T GetById(int? Id);
        void Insert(T entity);
        void Update(T entity);
        void Delete(T entity);
        void Save();
    }

存储库本身

public abstract class Repository<C,T> : IRepository<T> 
        where T : class
        where C : DbContext, new()
    {
        private C _context = new C();
        public C Context
        {
            get { return _context; }
            set { _context = value; }
        }


        public Repository(C context)
        {
            _context = context;
        }

        // Irrelevant data methods (...)
    }

当我尝试将DbContext特别传递给RegisterType()时,会发生错误。类似问题here

0 个答案:

没有答案