Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Code First 30+ seconds StartUp Time

I am aware that this has been asked too many times, but I couldn't figure out a solution from the asked questions.

I am fairly new to Entity Framework, and I developed a small CMS panel for my websites using Entity Framework 6 and Code First approach. My problem is, the website is really slow when first running (this is the website I am currently testing www.oakwoodpawnshop.ca) , it takes over 30 secs to load. After the first load, it takes about a sec to load other pages. I am getting 89/100 from PageSpeed insights,all my images /js /css files are minimized, and done some other front-end optimizations so I know that my problem is with EF. I have another website I've built about 3 years ago only using LINQ, no database optimization or html/image optimization, and it opens in about 3 seconds.

I have pre generated views using Entity Framework Power Tools, and I don't have any Include method in my codes. I filtered my queries before .toList() function.I probably have a problem with my codes or database optimization, but I am unable to come up with a solution through my research, because this area is new for me.

I've found an article in my native language asking someone with the same problem to run some codes on command prompt to see if the problem is with network settings.

These were the lines for command prompt:

1 - netsh int ip reset a.txt

2 - netsh winsock reset

3 - netsh winhttp reset proxy

4 - netsh advfirewall reset

5 - ipconfig /flushdns

6 - Restart your computer and check again.

When I did these and checked back to website, the whole website opened in 3 seconds. After a while, it started to have 30+ seconds start up time again. Obviously I can't run these commands on everyone's computer, but I am lost and I have no idea why its taking so long to start.

Here is an example of the method I am using:

public List<Categories> GetCategories()
    {
        List<Categories> c = db.Categories.Where(x => x.TopCategoryId == 0).OrderBy(x => x.CatOrder).ToList();

        return c;
    }

I created custom methods to fetch my datas(so I can use them more easily), under a 'Worker' class to gather all my methods in one class. Could that be the problem?

I'd be happy to provide any more information you need, and I am dealing this for about a month now with no luck, so I am hopelessly looking for an answer, or I will have to re-create whole application without EF, because there is no way a customer would wait for a website that has a +30 sec opening time.

Thank you in advance for all your advices!

like image 378
TC Seden Ergül Avatar asked Jun 18 '15 03:06

TC Seden Ergül


1 Answers

You can initialize Entity Framework database. Replace DbContext with your context class:

using(var context = new DbContext())
{
    context.Database.Initialize(false);
}

If you are using asp.net mvc, you can do this from your Application_Start() method. However, you need to use Application Initialization module (available for IIS 7.5 as extension). Modify the application pool entry so that the application pool is always running. Remove idle timeout. Configure IIS application pool recycling at specific time (04:00 AM, for example). You may need to specify initialization page in Web.config ('/' is default):

<applicationInitialization>
  <add initializationPage="/initialze" />
</applicationInitialization>

Those steps will not completely warm up Entity Framework, but will greatly reduce cold startup time.

like image 119
ranquild Avatar answered Sep 17 '22 13:09

ranquild