1

I have a single WCF Service that is going to be handling insert, updates and deletes (not reads) for my entities.

This service has several EF Models in it (each mapping to different parts of the database).

The question I have is this: Is creating ObjectContexts expensive? Can I create a new one each time a request for a save comes in (which will be fairly frequently)?

Or should I try to make each instance last longer than that? And if I do, does the context cache stuff? (I am going to be potentially putting this in a hardware load balancing situation.)

If I am going to be making new ObjectContexts for each request, is there a way I can make that quicker?

Vaccano
  • 70,257
  • 127
  • 405
  • 747
  • [This](http://stackoverflow.com/questions/5414076/ef4-objectcontext-lifetime) question is very similar and has some very good answers. – Bobby D Aug 09 '11 at 19:03

1 Answers1

3

Creating a new context is fast. The life time of a context in entity framework is designed to be as short as possible (unit of work pattern). Creating a instance per request is the recommed way. See here.

Web applications – use the context per request. Since in web applications we deal with requests that are very short but holds all the server transaction then they are the proper duration for the context to live in. We will enjoy all the functionality of the context and it won’t hang up very long.

Fox32
  • 11,159
  • 8
  • 47
  • 69