如何在 mvc 5 中使用 entity framework 实现依赖注入

How to implement dependency injection using entity framework in mvc 5

我正在尝试使用 entity framework 实现依赖注入,但它给出了 "An unhandled exception of type 'System.WhosebugException' occurred in Unity.Container.dll" 的异常并且应用程序进入了中断模式

public class CategoryRepository : ICategoryRepository
{
    private LaundryManagementSystemEntities context;
    private ICategoryRepository _iCategory;

    //public CategoryRepository(LaundryManagementSystemEntities db) //For repositoty Patterns or Unit of work
    //{
    //    this.context = db;
    //}

        //For dependency Injection
        public CategoryRepository(ICategoryRepository iCategory,LaundryManagementSystemEntities _context)
    {
        this._iCategory = iCategory;
        this.context = _context;
    }


    public void CreateCategory(CategoryViewModel categoryViewModel)
    {
        var category = new Category();
        category.CategoryName = categoryViewModel.CategoryName;
        category.IsActive = categoryViewModel.IsActive;
        context.Categories.Add(category);
        context.SaveChanges();
    }

这里正在制作类别

的存储库class
 public interface ICategoryRepository:IDisposable
{
    List<Category> GetCategories();
    Category GetCategoryById(int? categoryId);
    void CreateCategory(CategoryViewModel category);
    void DeleteProductOfCategory(int productId);
    void DeleteCategory(int categoryId);
    void PostEditCategory(CategoryViewModel category);
    CategoryViewModel GetEditCategory(int? categoryId);
}

这是一个界面

 public class CategoryController : AdminBaseController
{
    LaundryManagementSystemEntities db = new LaundryManagementSystemEntities();

    private ICategoryRepository interfaceobj;
    //private UnitOfWork unitOfWork;

    public CategoryController(ICategoryRepository iCategory)
    {
        this.interfaceobj = iCategory;

        //For Repositorypatterns
        //this.interfaceobj = new CategoryRepository(new LaundryManagementSystemEntities());
        //For Unit Of Work
       // this.unitOfWork = new UnitOfWork(new LaundryManagementSystemEntities());
    }

    // GET: Category        
    public ActionResult Index()
    {
        return View();
    }
    public ActionResult Create()
    {
        CategoryViewModel categoryViewModel = new CategoryViewModel();
        return PartialView("Create",categoryViewModel);
    }

    [HttpPost]
    public ActionResult Create(CategoryViewModel category)
    {
        if (ModelState.IsValid)
        {
            interfaceobj.CreateCategory(category);
           // unitOfWork.CategoryRepository.CreateCategory(catogery);
           // interfaceobj.CreateCategory(catogery);
        }
        return PartialView("Create",category);
    }

这是控制器

我没有遇到异常

我想正确地了解它以及它会如何 运行

ICategoryRepository 注入派生自同一接口的 CategoryRepository 正在创建 cyclic/circular 依赖性,这会导致堆栈溢出。

删除该依赖项。最初显示的代码似乎没有使用也不需要该依赖项。

public class CategoryRepository : ICategoryRepository {
    private readonly LaundryManagementSystemEntities context;

    public CategoryRepository(LaundryManagementSystemEntities context) {
        this.context = context;
    }

//...