Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
118 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...