简体   繁体   English

EntityFramework:通过工作单元模式为每个请求保留实体上下文

[英]EntityFramework: Hold entity context per request via Unit of Work pattern

there are so much information available about unit of work pattern, but most of them are different. 有关工作单元模式的信息很多,但大多数都不同。

I've learned that I should have for each request my own entity context. 我已经知道我应该为每个请求提供我自己的实体上下文。 And that I should use Unit of Work pattern to reach this goal (from here. Entity Framework and Connection Pooling ) 而且我应该使用工作单元模式来实现这个目标(从这里开始。 实体框架和连接池

So I've implemented it exactly this way: http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application 所以我用这种方式实现了它: http//www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-图案-在-AN-ASP-净-MVC的应用程序

But with only that implementation, I don't have one context per request, all requests are sharing the same context, right? 但是只有那个实现,每个请求我没有一个上下文,所有请求都共享相同的上下文,对吧?

Then I found this link: http://www.mindscapehq.com/blog/index.php/2008/05/12/using-the-unit-of-work-per-request-pattern-in-aspnet-mvc/ 然后我找到了这个链接: http//www.mindscapehq.com/blog/index.php/2008/05/12/using-the-unit-of-work-per-request-pattern-in-aspnet-mvc/

But now I remember that I should not hold the context in session / HttpContext.Items variable. 但现在我还记得,我应该持有会话/ HttpContext.Items变量的上下文。 Is that right? 那正确吗?

Where can I find a best implementation tutorial? 我在哪里可以找到最佳实施教程?

I follow the pattern given in your second link . 我按照第二个链接中给出的模式。 You are not sharing a context across all requests with that pattern. 您没有使用该模式在所有请求之间共享上下文。 Take a look at the sample code: 看一下示例代码:

public class StudentController : Controller
{
    private IStudentRepository studentRepository;

    public StudentController()
    {
        this.studentRepository = new StudentRepository(new SchoolContext());
    }
}

For each request to the Student controller, a new instance of that class will be created. 对于Student控制器的每个请求,将创建该类的新实例。 In the constructor of that class, it's creating a new repository instance with a new context. 在该类的构造函数中,它使用新上下文创建新的存储库实例。 That means that the context will live only for the lifetime of that request. 这意味着上下文仅在该请求的生命周期内存在。

Edit : Here's a little more clarification: 编辑 :这里有一点澄清:

Maybe stepping backward in the process would help clarify. 也许在这个过程中退步有助于澄清。 Start with a visitor hitting some action in your controller. 首先让访问者在您的控制器中执行某些操作。 ASP.NET is going to create an instance of your controller class. ASP.NET将创建一个控制器类的实例。 When it creates that instance, you will have a context in memory that will live for the duration of that request and no longer . 当它创建该实例时,您将在内存中拥有一个上下文,该上下文将在该请求的持续时间内生效,而不再存在

This works out just fine because you are performing work within your controller. 这很好,因为你在控制器中执行工作。 Say for an example that a user asks to update their profile. 比如用户要求更新其个人资料的示例。 How would you handle that with Entity Framework (EF)? 你会如何处理实体框架(EF)? First, you would use your repository and fetch their user record. 首先,您将使用您的存储库并获取其用户记录。 Your context is now aware of that object. 您的上下文现在知道该对象。 Then you take the data the visitor supplied (let's say they want to change their phone number) and update your EF object with the new value. 然后,您获取访问者提供的数据(假设他们想要更改他们的电话号码)并使用新值更新您的EF对象。 The context is keeping track of those changes, so at the end of your action you can call .Save() and the proper update will be made to your database. 上下文跟踪这些更改,因此在操作结束时,您可以调用.Save()并对数据库进行适当的更新。

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

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