Six seconds warmup time for the first entity framework 6 nonquery

前端 未结 4 1713
夕颜
夕颜 2021-01-05 23:51

From my integration test:

// Act
Stopwatch w = new Stopwatch();
w.Start();
userService.Create(userDTO);
w.Stop();


public void Create(UserDTO userDTO)
{
            


        
4条回答
  •  情深已故
    2021-01-06 00:33

    The time is not spent to insert a simple data. EF creates the model in the memory, that is where the time you spent goes.

    EF creates Entity Data Model and executes View Generation(not db views) for the first time you do an operation on context. Have a look at this blog post.

    Take a look here to improve the performance by using pre-generated views to decrease model load time.

    To improve the performance you can initialize your context async when you start your application. Beware the multithreading issues.

    using (var context = new MyContext())
    {
        context.Database.Initialize(false);
    }
    

提交回复
热议问题