Dennis
Dennis

Reputation:

Entity framework context as static

In a web application it would be ok if i declare the context of a entity framework model as static? it would be ok? its not recommended? why? Thanks!

Upvotes: 33

Views: 14242

Answers (3)

Alex James
Alex James

Reputation: 20924

Almost definitely not.

ObjectContexts get bigger and bigger as more Objects are queried / saved.

Also sharing an ObjectContext between threads, as you would be doing, is not recommended, because of the locking issues and undeterministic side-effects you would have to deal with.

I wrote a tip on this topic a while back.

Tip 18 - How to decide on a lifetime for your ObjectContext

Upvotes: 34

RichardOD
RichardOD

Reputation: 29157

This answer sort of answers your question, so does this one. I certainly wouldn't consider having it as static!

Rick Strahl has an in depth article on the lifecycle management.

Upvotes: 6

vidalsasoon
vidalsasoon

Reputation: 4401

I had done this the first time I implemented the entity framework. The problem was that the whole application was getting "completed" events so I had to do a lot of code figuring out where the call came from.

I decided to refactor so each page would have an instance of the context. I like it much better now.

/my experience

Upvotes: 2

Related Questions