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.
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.
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.
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.
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.
The delay is due to the time Entity Framework takes to fire up.
see this SO question for more info
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With