Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework is taking 3 seconds to execute the simplest query I've seen in my life for the first time. It becomes instantaneous thereafter

I use Entity Framework 4.5 with Object Context. My model has around 200 entities type. The first time I make a ridiculous query in a 10 records database that returns 1 record, it will take 3 seconds. From then on, not only this query, but all will become instant. If I close IISExpress and open it again. The first time will be very slow again.

There must be something I can do with Entity Framework. I've think of pregenerating views or upgrading to Entity Framework 5.0, but I'm not really sure this will help.

Any idea?

EDIT Queries executed directly in Management Studio are always fast. The query is very simple. I just get a user by username, and the table has like.. 10 records. Debug or release don't make much difference.

like image 820
André Pena Avatar asked Apr 17 '13 13:04

André Pena


People also ask

Why is Entity Framework first load slow?

Entity Framework loads very slowly the first time because the first query EF compiles the model. If you are using EF 6.2, you can use a Model Cache which loads a prebuilt edmx when using code first; instead, EF generates it on startup.

How does Entity Framework affect the connection with the database?

Because an open connection to the database consumes a valuable resource, the Entity Framework opens and closes the database connection only as needed. You can also explicitly open the connection. For more information, see Managing Connections and Transactions. Once in each application domain.

Which is better ado net or Entity Framework?

If we want to achieve more control over SQL commands and operations with the help of raw SQL Queries, then ADO.NET will be a great choice to start work with. Whereas if we want to develop the application in a much faster way with clear code maintainability, then Entity Framework will be the better choice.


2 Answers

This is a known "issue". I put that in quotes because it's an issue for you and me, but Microsoft considers it normal and not a problem. This MSDN Page shows the performance costs of various parts of EF.

There are already many, many, many questions on SO about this. I would suggest exploring some of them. There was a particular question a year or two ago in which a pretty detailed analysis of the problem was done by multiple people, but I can't seem to find it right now.

In particular this is very slow with large models and EDMX files, converting to a code first model could help.

like image 107
Erik Funkenbusch Avatar answered Oct 12 '22 23:10

Erik Funkenbusch


The delay is due to the time Entity Framework takes to fire up.

see this SO question for more info

like image 27
qujck Avatar answered Oct 13 '22 00:10

qujck