Entity Framework 6.2 very slow first startup and EFInteractiveViews

前端 未结 2 1988
甜味超标
甜味超标 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:10

    I want to know if these issues are updated in EF 6.2.0 or method of fixing it has changed, or any thing im doing wrong/should check.

    The answer is yes,

    EF 6.2 has introduced a Model Cache

    public class MyDbConfiguration : DbConfiguration
    {
        public MyDbConfiguration() : base()
        {
            var path = Path.GetDirectoryName(this.GetType().Assembly.Location);
            SetModelStore(new DefaultDbModelStore(path));
        }
    }
    
    [DbConfigurationType(typeof(MyDbConfiguration))]
    public class MyContextContext : DbContext 
    {
    }
    

    You can learn more here: https://codeopinion.com/entity-framework-code-first-model-cache/

    0 讨论(0)
  • 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.

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