问题描述
我得出结论,实体上下文应被视为工作单位,因此不被重用。很好,
但是,为了加快我的数据库访问速度,我遇到了这个博文...
该帖子认为,EFs与其他框架相比的性能差,往往是由于每次新建 EntityConnection em> EntityContext 对象。
为了测试这个,我手动创建了一个静态EntityConnection,在Global.asax.cs中 Application_Start( )
然后,我将所有上下文使用语句转换为
using(MyObjContext currContext = new MyObjeContext(globalStaticEFConnection)
{
....
}
这似乎已经加快了一点,没有任何错误,只要我能告诉。
但是否安全?
使用应用范围内的静态实体连接是否引入竞争条件?
最好的问候,
Kervin
EntityConnection被记录为不是线程安全的。我想你可以汇集他们,但你不能使用一个单一的静态连接的Web应用程序,因为将有很多线程涉及。
There have been many question on managing EntityContext lifetime,
e.g. 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 Entity Framework Performance
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.
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.
But is this safe?
Does using a applicationwide static EntityConnection introduce race conditions?
Best regards,Kervin
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生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!