Take the 2-minute tour ×

I have a problem with a quite complex query executed through Entity Framework that takes so much time, almost 50 seconds. The query is executed with an ad-hoc call to a web service which creates a new ObjectContext, execute the query and returns the result.

The problem is that if I trace with SQL Server Profiler the T-SQL code and try to execute it from SQL Server Management Studio it takes like 2 seconds... what could it be?

Thank you, Marco

share|improve this question

1 Answer 1

For every ObjectContext that touches the database, Entity does a lot of startup work building an internal representation of the database schema. This can take a long time (our project is about 30 seconds), and is rolled into the expense of the first query made against the database. Subsequent ones are plenty fast, until the process is restarted. Does that apply to you?

share|improve this answer
but if I declare a new objectcontext entity framework recreate the database schema? because i don't restart the application –  Mackho Feb 10 '12 at 18:27
"new ObjectContext" won't cause the metadata to be re-created. However, if IIS is killing the process or something every time... You need to post some code so we can see whats going on. Need to see how you're executing the query on both the client and server. –  insta Feb 10 '12 at 19:15
Sorry I can't post code because it is protected and I am not allowed to, but basically the client call the service that create a new class in the DAL layer which instances the object context and makes the query. The objectcontext will be disposed but IIS doesn't kill the process... –  Mackho Feb 10 '12 at 20:36

Your Answer


