EF6/Code First: Super slow during the 1st query, but only in Debug

entity-framework entity-framework-6 visual-studio-2013

Question

I'm using EF6 rc1 with Code First strategy, without precompiled views and the problem is: If I compile and run the exe application it takes like 15 seconds to run the first query (that's okay, since I'm still working on the pre-generated views). But if I use Visual Studio 2013 Preview to Debug the exact same application it takes almost 2 minutes BEFORE running the first query:

Dim Context = New MyEntities()
Dim Query = From I in Context.Itens '' <--- The debug takes 2 minutes in here
Dim Item = Query.FirstOrDefault()

Is there a way to remove this extra time? Am I doing something wrong here?

Ps.: The context itself is not complicated, its just full with 200+ tables.

Edit: Found out that the problem is that during debug time the EF appears to be generating the Views ignoring the pre-generated ones. Using the source code from EF I discovered that the property:

IQueryProvider IQueryable.Provider
    {
        get
        {
            return _provider ?? (_provider = new DbQueryProvider(
                                                 GetInternalQueryWithCheck("IQueryable.Provider").InternalContext,
                                                 GetInternalQueryWithCheck("IQueryable.Provider").ObjectQueryProvider));
        }
    }

is where the time is being consumed. But this is strange since it only takes time in debug. Am I missing something here?

Edit: Found more info related to the question: Using the Process Monitor (by Sysinternals) I found out that there its the 'desenv.exe' process that is consuming tons of time. To be more specific its consuming time with an 'Thread Exit'. It repeats the Thread Exit stack 36 times. I don't know if this info is very useful, but I saved a '.cvs' with the stack, here is his body: [...] (edit: removed the '.cvs' body, I can post it again by the comments if someone really think its going to be useful, but it was confusing and too big.)

Edit: Installed VS2013 Ultimate and Entity Framework 6 RTM. Installed the Entity Framework Power Tools Beta 4 and used it to generate the Views. Nothing changed... If I run the exe it takes 20 seconds, if I 'Start' debugging it takes 120 seconds.

Edit: Created a small project to simulate the error: http://sdrv.ms/16pH9Vm Just run the project inside the environment and directly through the .exe, click the button and compare the loading time.

Accepted Answer

This is a known performance issue in Lazy (which EF is using) when the debugger is attached. We are currently working on a fix (the current approach we are looking at is removing the use of Lazy). We hope to ship this fix in a patch release soon. You can track progress of this issue on our CodePlex site - http://entityframework.codeplex.com/workitem/1778.

More details on the coming 6.0.2 patch release that will include a fix are here - http://blogs.msdn.com/b/adonet/archive/2013/10/31/ef6-performance-issues.aspx




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why