问题描述
已经有许多问题在管理的的EntityContext 的一生中,
There have been many question on managing EntityContext lifetime,
例如。 http://stackoverflow.com/questions/813457/instantiating-a-context-in-linq-to-entities
我已经到了实体方面应该被视为一个单位-的工作,因此不重用的结论。太好了。
I've come to the conclusion that the entity context should be considered a unit-of-work and therefore not reused. Great.
不过,在做一些研究,对于加快我的数据库访问,我遇到了这个博客帖子...
But while doing some research for speeding up my database access, I ran into this blog post...
Improving实体框架的性能
该帖子称,EF的糟糕表现相对于其他框架往往是由于的 EntityConnection 的对象,被每一次创造了一个新的的EntityContext 的目标是必要的。
The post argues that EFs poor performance compared to other frameworks is often due to the EntityConnection object being created each time a new EntityContext object is needed.
要测试这一点,我手动创建一个静态EntityConnection中的的Global.asax.cs 的的Application_Start()。
To test this I manually created a static EntityConnection in Global.asax.cs Application_Start().
然后我转换我的所有情况下的使用的语句
I then converted all my context using statements to
using( MyObjContext currContext = new MyObjeContext(globalStaticEFConnection)
{
....
}
这似乎已经加快了一点东西,没有任何错误,到目前为止,据我可以告诉。
This seems to have sped things up a bit without any errors so far as far as I can tell.
但是,这是安全的吗?
是否使用应用程序范围静态的 EntityConnection 的引入竞争条件?
Does using a applicationwide static EntityConnection introduce race conditions?
最好的问候,Kervin
Best regards,Kervin
推荐答案
EntityConnection是记录将不是线程安全的。我想你可以集中他们,但你不能使用Web应用程序的单一,静态连接,因为会涉及多个线程。
EntityConnection is documented to be not thread-safe. I think you could pool them, but you cannot use a single, static connection for a Web application, as there will be many threads involved.
这篇关于管理EntityConnection一生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!