Page 1 of 1

First time model load is slow

Posted: Wed 08 Feb 2012 09:08
by magnus
The answer to this question may very well be "that's to be expected", but it can't hurt to ask.

We have a quite large model. There are roughly 200 entities, and some of the entities are themselves pretty big -- the average number of columns per entity is probably in the 10-20 range. We're using the DbContext template with database independent and fluent mapping set to true, and the generated OnModelCreating method is about 15,000 lines. We've customized the DbContext template somewhat, but I don't think there's anything we've done that should have an effect on any of this. We're using Entity Framework 4.2.

Right after the application starts, we create a DbContext and perform a dummy query to ensure that everything gets loaded. It takes over 20 seconds for the line with the dummy query to execute. The vast majority of that time is spent somewhere inside EntityFramework.dll. I can say for certain that it's not the OnModelCreating method itself that's the problem, as it executes in less than a second on my computer.

All subsequent Entity Framework operations are nice and quick, so this isn't the end of the world. Basically I'm just wondering if I have to expect this kind of load time with a model of my size, or if there's something you know about that I can do to shorten the load time?

Posted: Thu 09 Feb 2012 14:41
by Pinturiccio
The executing of the first query in large Entity Framework models usually takes a long time because of view generation (this and other reasons of the delay is described in http://msdn.microsoft.com/en-us/library/cc853327.aspx).
Are you sure that you need database independent fluent mapping? If not, and you do not use model with few different DBMS (for example, with SQL Server and Oracle), then database independent fluent mapping can be disabled. Mapping code size will be reduced and performance will be improved.

If you use only one DBMS, then you can try use EF Power Tools CTP1 (http://blogs.msdn.com/b/adonet/archive/ ... eased.aspx), but this tool has some problems (some aspects are described on http://www.devart.com/blogs/dotconnect/ ... qlite.html)

Also you can disable one or more of default conventions. Each convention is resource-demanding, because it does a lot of excessive checks, and the fluent mapping that we generate by default, covers all cases completely. For more information please refer to:
http://msdn.microsoft.com/en-us/library ... 03%29.aspx
http://msdn.microsoft.com/en-us/library ... 03%29.aspx

Posted: Mon 13 Feb 2012 08:30
by esben
Any known way of pre-generating the view using Code-First (Fluent mapping that is)?

Posted: Mon 13 Feb 2012 16:28
by Shalex
Refer to http://blogs.msdn.com/b/adonet/archive/ ... eased.aspx, the Optimize Entity Data Model section (but take into account our comments in the previous post).

Posted: Wed 15 Feb 2012 09:06
by magnus
We've tested the view generation, and on my computer it cuts the load time from 23 to 13 seconds, which is obviously good news. Thanks for the tip!

I should probably refer this question to Microsoft, but do you happen to have any idea why the generated EntityViewContainer class has to be in the same assembly as the database entities? We keep our entities and data context objects in separate assemblies, and since the default location of the generated view class was of course the assembly with the context class, we couldn't get the generated view to work at first. Is there some way to avoid this behavior, for instance by explicitly passing the view class to some built-in method?

Posted: Fri 17 Feb 2012 17:22
by Shalex
magnus wrote:EntityViewContainer class has to be in the same assembly as the database entities?
Yes. This is a peculiarity of EF engine behaviour when working with the pregenerated views. We cannot change it on our side.
magnus wrote:Is there some way to avoid this behavior, for instance by explicitly passing the view class to some built-in method?
Please ask this question to Microsoft team.