A friend of mine has found the following problem with Entity Framework 4:
- Two simple classes and one association between them (one to many):
- One condition to filter out soft-deleted entities (WHERE Deleted = 0):
- 100 records in the database;
- A simple query:
Will produce the following SQL:
And will result in 680 MB of memory being taken!
Now, Entity Framework has been historically known for producing less than optimal SQL, but 680 MB for 100 entities?!
According to Microsoft, the problem will be addressed in the following version, there is a Connect issue open. There is even a whitepaper, Performance Considerations for Entity Framework 5, which talks about some of the changes and optimizations coming on version 5, but by reading it, I got even more concerned:
“Once the cache contains a set number of entries (800), we start a timer that periodically (once-per-minute) sweeps the cache.”
Say what?! The next version of Entity Framework will spawn timer threads?!
When Code First came along, I thought it was a step in the right direction. Sure, it didn’t include some things that NHibernate did for quite some time – for example, different strategies for Id generation that do not rely on IDENTITY columns, which makes INSERT batching impossible, or support for enumerated types – but I thought these would come with the time. Now, enumerated types have, but so did… timer threads! I’m afraid Entity Framework is becoming a monster.