Entity Framework 6.2 very slow first startup and EFInteractiveViews

前端 未结 2 1987
甜味超标
甜味超标 2021-01-24 15:47

this topic is already widely discussed on stackoverflow and many other blogs, reason to asking question is that i observe this topic was discussed in mostly 3 to 5 years old pos

2条回答
  •  一生所求
    2021-01-24 16:27

    I assume you read the performance considerations:

    https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/ef/performance-considerations

    I am personally struggling with these issues aswell. I resorted to a) creating custom SQL for some queries. and b) I created a warmup mechanism. There is also an option to precompile queries which might help.

    The warmup mechanism runs at the start of the application on a seperate thread. There it does a very simple request to the database, this forces the creation of the model, and 'triggers' the initial startup delay for all queries. After that query has ran (it takes a couple seconds) a flag gets set. All other operations wait for the flag being set before running. Especially in a multithreaded scenario this helps a lot.

    I found out that the startup delay was created per thread. This means that if the delay is let's say 2 seconds, running 3 threads who all create a context and try to run some linq, make the whole application wait for 6 seconds.

    Note that by creating custom queries, one must return all fields for the entity to be created;

        var blogs = context.Blogs.SqlQuery("SELECT * FROM dbo.Blogs").ToList(); //works
        var blogs = context.Blogs.SqlQuery("SELECT [id] FROM dbo.Blogs").ToList(); //Does not.
    

    I have not tested this matter fully - so take some time to test. This is just a heads up.

提交回复
热议问题