本文介绍了管理的DbContext的寿命的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个的DbContext的一生绑在会话的生存期,以 - 例如 - 能够提交或上的DbContext在多个请求放弃对一组突变的变化

I would like to tie the lifetime of a dbContext to the lifetime of a session, to - for example - be able to commit or abandon changes on a group of mutations on the dbcontext over multiple requests.

还有没有其他的(更好吗?)的方式来做到这一点?如果没有,你会是一个合适的机制来创建和上下文的处置?我想与会话结束清除静态哈希表,但也许我做的都是错的。我还想着只持有那些有过多次要求做的工作环境,并保持每个动作,其余的想法。有什么建议?

Are there any other (better?) ways to accomplish this? If no, what would be a suitable mechanism to create and dispose of the contexts? I am thinking about static hashtables with cleanup on session end, but maybe I'm doing it All Wrong. I am also thinking about the idea of only holding on to those contexts that have to do work over multiple requests, and keeping the rest per action. Any advice?

推荐答案

您可以使用 IOC(控制反转)容器管理的DbContext的生命周期,如 StructureMap 具有以下步骤:

You can use IoC(Inversion of Control) containers to manage the lifetime of DBContext such as StructureMap with following steps :


  1. 有关MVC 4安装的NuGet包:

阅读快速启动:

设置你的DbContext范围:

Set scope of your DBContext :http://docs.structuremap.net/Scoping.htm

您也可以在的DbContext在多个请求中使用库和单位的工作模式中的组合提交或放弃变动对一组突变。

Also you can use the combination of Repository and Unit Of Work Patterns for commit or abandon changes on a group of mutations on the dbcontext over multiple requests.

这篇关于管理的DbContext的寿命的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 18:23