Unit of Work pattern implementation

前端 未结 3 509
悲&欢浪女
悲&欢浪女 2020-12-28 20:58

I am creating an application with ASP.NET MVC and Entity framework code first. I am using repository and unit of work pattern with influence of from following link.

3条回答
  •  既然无缘
    2020-12-28 21:57

    Try passing the SchoolContext to the GenericRepository:

    public GenericRepository
    {
        private SchoolContext _context;
    
        public GenericRepository(SchoolContext context)
        {
           _context = context;
        }
    
        public Get(int id)
        {
            return _context.Set().Find(id);
        }
    }
    

    And use:

    using(var context = new SchoolContext())
    {
        var departmentRepository = new GenericRepository(context);
        var department = departmentRepository.Get(1);
    }
    

提交回复
热议问题