管理EntityConnection生命周期 [英] Managing EntityConnection lifetime

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

问题描述



实例化LINQ to Entities中的上下文



我得出结论,实体上下文应被视为工作单位,因此不被重用。很好,



但是,为了加快我的数据库访问速度,我遇到了这个博文...



改进实体框架性能



该帖子认为,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生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆