OWIN DbContext and a single DbContext for all of my Repositories

匆匆过客 提交于 2020-01-04 19:38:56

问题


I am building a ASP.NET MVC application. I'm new to the whole Repositories/Unit Of Work patterns. I'm part way through implementing a generic repository for my EF objects.

I realise that there is an issue with ensuring that you only have a single DbContext per Http request... I'm pretty sure that I can achieve this by creating an instance using OWIN.

    /// <summary>
    /// Configures OWIN
    /// </summary>
    /// <param name="app"></param>
    public void Configuration(IAppBuilder app)
    {
        // Create instance of database context
        app.CreatePerOwinContext<QuizEntitiesDbContext>(GetQuizEntitiesDbContext);
    }

    /// <summary>
    /// Returns an instance of the QuizEntitiesDbContext database context
    /// </summary>
    /// <returns></returns>
    public static QuizEntitiesDbContext GetQuizEntitiesDbContext()
    {
        return new QuizEntitiesDbContext();
    }

Now that seems fine if I want to get that instance within my controller, but really I need to be able to access this instance through my generic repository...

  • Is there a way that this can be achieved?

I'm also using Ninject dependency injector in my project and I've read that I can get an instance of objects per HTTP request for use in my application, something along the lines of this:

this.kernel.Bind(IMyDbContext).To(MyDbContext).InRequestScope();
  • Should I be using this/OWIN or both?
  • Do I need to wrap my DbContext in an interface or something?

来源:https://stackoverflow.com/questions/25827376/owin-dbcontext-and-a-single-dbcontext-for-all-of-my-repositories

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