简体   繁体   English

如何设置我的项目以根据http请求创建实体框架上下文?

[英]How do I set my project to create the entity-framework context per http request?

I am working on a C# MVC5 project using EF6. 我正在使用EF6开发C#MVC5项目。 Currently the context is created in the UnityConfig.cs class, which is called from the Application_Start() in Global. 当前,上下文是在UnityConfig.cs类中创建的,该类从Global中的Application_Start()调用。 See some of use UnityConfig class below: 请参阅下面的一些使用UnityConfig的类:

public static void RegisterComponents()
{
   var container = new UnityContainer();
   BlogSite.Domain.BlogSiteModelContainer context = new Domain.BlogSiteModelContainer();
   container.RegisterType<IBlogRepository, BlogRepository>(new InjectionConstructor(context));
}

The problem with this is that the database may be updated directly and so the EF context will become inconsistent. 问题是数据库可能会直接更新,因此EF上下文将变得不一致。 So I need to change it so that it is re-loaded each time there is a new http request. 所以我需要更改它,以便每次有新的http请求时都重新加载它。

I have found that PerRequestLifetimeManager may be the solution for this, however, I can't find a good example of how I should use this. 我发现PerRequestLifetimeManager可能是解决此问题的方法,但是,我找不到如何使用此方法的好例子。

How should I go about implementing this? 我应该如何实施呢? Or is there a better way? 或者,还有更好的方法?


So I have tried a few things but still not getting anywhere. 因此,我尝试了一些尝试,但仍无所获。 I realise I can add in a lifetime manager with the container.RegisterType() method, but I assume that only applies to the repository and not the Entity Framework context. 我意识到我可以使用container.RegisterType()方法添加一个生命周期管理器,但是我假设这仅适用于存储库,不适用于Entity Framework上下文。

eg 例如

container.RegisterType<IBlogRepository, BlogRepository>(new PerRequestLifetimeManager(), new InjectionConstructor(context));

I'm still not sure how set the lifetime of the context. 我仍然不确定如何设置上下文的生存期。 I realise I'll probably have to re-format the code a bit but I can't find an example of this situation anywhere. 我意识到我可能不得不重新格式化代码,但是我在任何地方都找不到这种情况的示例。


I've been doing more research, and have looked at the idea of creating a Unit of Work to create the context within and pass it to the repositories. 我一直在做更多的研究,并研究了创建工作单元以在其中创建上下文并将其传递到存储库的想法。 Not sure if its the best way forward, but it seems to be one that might work. 不确定这是否是最好的方法,但似乎可行。 Any thoughts on that? 有什么想法吗?

Found the solution. 找到了解决方案。 I needed to create a UnitOfWork class which I created the context within and the repositories. 我需要创建一个UnitOfWork类,并在其中创建上下文和存储库。 I then used Unity to inject the UnitOfWork class into the controllers with the PerRequestLifetimeManager lifetime manager. 然后,我使用Unity通过PerRequestLifetimeManager生命周期管理器将UnitOfWork类注入到控制器中。

So my UnitOfWork class (which is stored in my DAL project) is: 因此,我的UnitOfWork类(存储在我的DAL项目中)是:

public class UnitOfWork : IDisposable
{
    private static BlogSite.Domain.BlogSiteModelContainer context;
    public IBlogRepository blogRepository;

    public UnitOfWork()
    {
        context = new BlogSite.Domain.BlogSiteModelContainer();;
        blogRepository = new BlogRepository(context);
    }

    public void Save()
    {
        context.SaveChanges();
    }

    private bool disposed = false;

    protected virtual void Dispose(bool disposing)
    {
        if (!this.disposed)
        {
            if (disposing)
            {
                context.Dispose();
            }
        }
        this.disposed = true;
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }
}

And I register this simply in my UnityConfig class with the following: 然后,我使用以下代码在我的UnityConfig类中简单地注册它:

container.RegisterInstance<UnitOfWork>(new UnitOfWork(), new PerRequestLifetimeManager());

In my controllers I then just had a few small changes to make to the constructors so that they would accept the UnitOfWork rather than the repositories. 然后,在我的控制器中,我对构造函数进行了一些小的更改,以使它们可以接受UnitOfWork而不是存储库。

Eg 例如

IBlogRepository blogRepo;

public BlogController(UnitOfWork unitOfWork)
{
   blogRepo = unitOfWork.blogRepository;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM