7-second EF startup time even for tiny DbContext

后端 未结 2 1224
借酒劲吻你
借酒劲吻你 2021-02-14 04:50

I am trying to reduce the startup time of my EF-based application, but I find that I cannot reduce the amount of time taken for an initial read below 7 seconds even for a single

相关标签:
2条回答
  • 2021-02-14 05:23

    As per several comments, the reason the second runs so fast is due to the connection manager caching the connections. Which leads me to believe that getting that initial connection is the problem.

    Things to try:

    1. Change your connection string to use the IP address of the db server instead of it's name.

    2. Connect to the database server using SQL Management Studio using the exact same credentials and mechanism. Do so from the machine that takes 7 seconds. Assuming that takes awhile, investigate to see what protocols are in use and ensure that the one you want is the first in the list.

    3. Put a network analyzer on your machine and watch what it does.

    0 讨论(0)
  • 2021-02-14 05:26

    After taking the code your wrote putting it in its own project I found that the project's platform target had a great effect on the start up time of the EF framework.

    When targeting an x64 platform, I received results similar to yours (7 second spin up on the first DbContext and <1 second on the second). When targeting x86 the spin up time for the first DbContext gets reduced by about 4 seconds down to 3.34633 seconds while the second DbContext takes a similar amount of time as in the x64 case.

    I am not sure why this happens but it must have to do with how the Entity Framework initializes itself in different environments. I have posted a separate question about that here.

    0 讨论(0)
提交回复
热议问题