none
Precompiling (creating a mapping?) EF DbContext for faster debugging RRS feed

  • Question

  • It takes a while (3 minutes+) to 'create/compile' my DbContext. The web server starts in about 5 seconds, but when I do the first query to my database, Entity Framework has to 'build/create/compile' the database in memory or something, I think? The next requests are almost instant. This was a Database First creation of the DbContext, the database already exists in MSSQL and has data. The DbContext contains about 500 DbSet's with relations.

    Is there a way to speed this up by doing the 'creation(mapping?)' of my Entity Framework's DbContext before running the web server, create the file/mappings it needs, so the first request is fast too?

    Friday, April 22, 2016 7:30 AM

Answers

  • Hi ICT ZInkendaal,

    >>Is there a way to speed this up by doing the 'creation(mapping?)' of my Entity Framework's DbContext before running the web server, create the file/mappings it needs, so the first request is fast too?

    The very first time any query is made against a given model, the Entity Framework does a lot of work behind the scenes to load and validate the model. We frequently refer to this first query as a "cold" query.  Further queries against an already loaded model are known as "warm" queries, and are much faster.

    the following blog show you three steps for massively improved first query performance.

    1. Using a cached db model store

    2. Generate pre-compiled views

    3. Generate pre-compiled version of entityframework using n-gen to avoid jitting

    The link provide a code-first sample, but it's similar to database-first.

    https://www.fusonic.net/en/blog/3-steps-for-fast-entityframework-6.1-code-first-startup-performance/

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. 

    Microsoft does not control these sites and has not tested any software or information found on these sites;Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there.

    There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Friday, April 22, 2016 9:02 AM
    Moderator

All replies

  • Do you REALLY need all 500 entities in the same model/DbContext - I doubt it.. (The more entities, the longer the initial "load" time.

    Also make sure to use the latest EF version (6.1.3 currently)


    Please mark as answer, if this was it. Visit my SQL Server Compact blog http://erikej.blogspot.com

    Friday, April 22, 2016 7:51 AM
  • I am using version 6.1.3 and version 7.0.0-rc1-final of EntityFramework.MicrosoftSqlServer.

    I do need alot of those tables, because they are related to eachother. Entity Framework could be used for huge projects according to the documents, so 500 does not seem too much.

    Friday, April 22, 2016 8:38 AM
  • Hi ICT ZInkendaal,

    >>Is there a way to speed this up by doing the 'creation(mapping?)' of my Entity Framework's DbContext before running the web server, create the file/mappings it needs, so the first request is fast too?

    The very first time any query is made against a given model, the Entity Framework does a lot of work behind the scenes to load and validate the model. We frequently refer to this first query as a "cold" query.  Further queries against an already loaded model are known as "warm" queries, and are much faster.

    the following blog show you three steps for massively improved first query performance.

    1. Using a cached db model store

    2. Generate pre-compiled views

    3. Generate pre-compiled version of entityframework using n-gen to avoid jitting

    The link provide a code-first sample, but it's similar to database-first.

    https://www.fusonic.net/en/blog/3-steps-for-fast-entityframework-6.1-code-first-startup-performance/

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. 

    Microsoft does not control these sites and has not tested any software or information found on these sites;Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there.

    There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Friday, April 22, 2016 9:02 AM
    Moderator
  • @Cole Wu

    So what if one is not using Code Frist?

    Friday, April 22, 2016 10:52 AM
  • Hi ICT ZInkendaal,

    As Data-First approach, we could use the following steps to pre-generate views to improve query performance

    1. generate model and mapping files for the model in the output directory

    2. add view generation to a Visual Basic project

    3. re-add mapping and model files as embedded resources for projects

    The following link provide a detailed demo about pre-generate views to improve query performance, please refer to:

    https://msdn.microsoft.com/en-us/library/bb896240.aspx

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, May 20, 2016 1:40 AM
    Moderator