0

First of all, sorry for my English.

I have a project with EF 6 and ASP.NET MVC 5, I've enable automatic migration, but every time I make the first query (my login), it takes a long time to run, one minute approx. I've debugged the code and I see that the models are rebuilt again and I don't know if this should happen. I've tried to disable automatic migration:

 AutomaticMigrationsEnabled = false;

In the configuration class and also:

  Database.SetInitializer<Models.CardCotaContext>(new CreateDatabaseIfNotExists<Models.CardCotaContext>());

in the Global.asax class, but nothing.

I'm worried because the same thing happens on the server.

Thanks.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
MCarmona
  • 11
  • 2
  • If you're manually adding the migrations with `add-migration` and `update-database` in the package manager console, you don't need to set the initializer. Does the problem happen if you comment it out? – Ortund Jul 21 '17 at 06:46
  • Yes, if I comment the line, the same thing happens. I've comment protected override void OnModelCreating(DbModelBuilder modelBuilder) too, but nothing. – MCarmona Jul 23 '17 at 03:52

1 Answers1

0

EF caches a lot of data in the AppDomain and currently executing thread. This happens when the first query is made (a cold query). This has the disadvantage that the first query is the slowest.

There are details of how the caching works in this link https://msdn.microsoft.com/en-us/library/hh949853(v=vs.113).aspx

IIS shuts down its worker process after a period of inactivity, this unloads the AppDomain forcing EF to run a cold query on the next hit. This also happens when a change is made to the bin folder or the web.config file on the server. So expect a cold query when you do a release or the AppPool has shut down.

If you are using a modern version of IIS (7.5+) there is a pre-initialisation feature that allows you to perform overlapped warm up when a pool is being restarted. Check this answer for details: How to warm up an ASP.NET MVC application on IIS 7.5?

In answer to your question, yes it is normal behaviour and the two links above will give you some tools to mitigate the cost of that startup

Slicksim
  • 7,054
  • 28
  • 32