结构图实体框架4连接
我正在为我的实体框架 4 个实体使用以下 Structuremap 引导代码:
x.For<XEntities>().LifecycleIs(Lifecycles.GetLifecycle(InstanceScope.PerRequest)).Use(() => new XEntities());
但是当我几乎同时执行两个请求时,我收到以下异常:
EntityException:The underlying provider failed on Open.
{"The connection was not closed. The connection's current state is connecting."}
我正在使用 ASP.NET MVC 2,在我的 Application_Start() 中有以下内容 EndRequest += new EventHandler(MvcApplication_EndRequest);
void MvcApplication_EndRequest(object sender, EventArgs e)
{
ObjectFactory.ReleaseAndDisposeAllHttpScopedObjects();
}
我可以做什么来解决这个问题?
[编辑] 这种情况发生在包含多个图像的页面上。图像来自数据库,由控制器操作提供服务,控制器操作从数据库读取图像,并将其作为文件结果发送到浏览器。我认为 asp.net 正在破坏我的对象上下文,并在图像请求传入时关闭我的数据库连接,并引发异常。
我现在需要的是一种正确的方法来以良好的方式管理对象上下文的生命周期。
I am using the following Structuremap bootstrapping code for my entity framework 4 entities:
x.For<XEntities>().LifecycleIs(Lifecycles.GetLifecycle(InstanceScope.PerRequest)).Use(() => new XEntities());
But when I do two nearly simultaneous requests, I get the following exception:
EntityException:The underlying provider failed on Open.
{"The connection was not closed. The connection's current state is connecting."}
I am using ASP.NET MVC 2, en have the following in my Application_Start()
EndRequest += new EventHandler(MvcApplication_EndRequest);
void MvcApplication_EndRequest(object sender, EventArgs e)
{
ObjectFactory.ReleaseAndDisposeAllHttpScopedObjects();
}
What can I do to fix this?
[edit]
this happens on a page with several images on it. The images come from the database, served by an Controller Action, which reads the image from the database, and sends it as a file result to the browser. I think that asp.net is breaking down my objectcontext, and closing my db connection when the requests for the images come in, and the exception is thrown.
What I need now, is a correct way to manage the lifetime of the object context in the good way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么要在 Application_Start() 中为 EndRequest 分配委托?
只需直接挂钩事件:
另外,我以前从未使用过该语法,这就是我的做法:
另外,您在什么时候更新您的数据上下文?你能展示一些代码吗?
Why are you assigning a delegate for EndRequest in Application_Start()?
Just hook directly into the event:
Also, i have never used that syntax before, this is how i do it:
Also, at what point do you new up your Data Context? Can you show some code?