Six seconds warmup time for the first entity framework 6 nonquery


Question

From my integration test:

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


public void Create(UserDTO userDTO)
{
    var user = userDTO.ToEntity();
    _context.Entry(user).State = EntityState.Added;
    _context.SaveChanges();
}

6,2 seconds to do an "sql insert" is crazy. I already see the application users complaining when they first open a project which they use the whole year. So everyday they have to wait 6 seconds...

I thought the warm up time in EF6 has improved?

Is there anything I can do to improve this miserable behavior?

Accepted Answer

Try EF6 CodeFirst View Generation T4 Template for C#. Pre-generated views improve application start-up time by moving the work that would have to be done at runtime to design time. more info


Popular Answer

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);
}




Licensed under: CC-BY-SA
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why