'Entity Framework Core too slow on First call

I have around 40 entities and everytime after I compile and run my app it takes almost 10 seconds whenever DbContext is called for the first time. Is there a way to make this run faster ?

This is what i do on the first call when the user is already logged in my app

Page Model

public class CreateModel : PageModel
{
    private readonly IToastNotification _toastNotification;
    private readonly ICelulaService _celulaService;

    public CreateModel(IToastNotification toastNotification, ICelulaService celulaService)
    {
        _toastNotification = toastNotification;
        _celulaService = celulaService;
    }

    public IList<Celula> Celulas { get; set; }

    public void OnGet()
    {
        Celulas = _celulaService.GetAutomated();
    }
}

Service and interface

public interface ICelulaService
{
    IList<Celula> GetAutomated();
}

public IList<Celula> GetAutomated()
{
    return _context.Celulas
         .Where(c => c.Automated)
         .ToList();
}

the Model

[Table("hCelulas")]
public class Celula
{
    public int Id { get; set; }
    [Required]
    [MaxLength(10)]
    [Display(Name = "Célula")]
    public string Nome { get; set; }


    public bool Automated { get; set; }

    public int? CheckListId { get; set; }
    public CheckList CheckList { get; set; }
}

database context

public class DatabaseContext : DbContext
{
   public DatabaseContext(DbContextOptions<DatabaseContext> options) : base(options)
   {

   }

   protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
   {

   }

   protected override void OnModelCreating(ModelBuilder modelBuilder)
   {    
        modelBuilder.Entity<Celula>()
            .HasIndex(x => x.Nome)
            .HasName("IX_Nome_Index")
            .IsUnique();

        modelBuilder.Entity<Celula>().HasData(
            new Celula { Id = 1, Nome = "3.9.X", Automated = true, },
            new Celula { Id = 2, Nome = "3.9.Y", Automated = true, },
            new Celula { Id = 3, Nome = "3.9.Z", Automated = true, }
        );

   }
   public DbSet<Celula> Celulas { get; set; }       
}

and on startup

services.AddDbContext<DatabaseContext>(options =>
{                       options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"), sqlServerOptionsAction: sqlOptions =>
      {
          sqlOptions.EnableRetryOnFailure(
              maxRetryCount: 2,
              maxRetryDelay: TimeSpan.FromSeconds(1),
              errorNumbersToAdd: null);
      });
 });

connection string

"DefaultConnection": "Server=(localdb)\mssqllocaldb;Database=DatabaseTest;Trusted_Connection=True;",

UPDATE

Added some data, this is basically the data i had when i was experiencing the slowness.



Solution 1:[1]

In EF-core 6.0 a new feature was added that allows using a compiled model.

The compiled model can be generated by a dotned command:

dotnet ef dbcontext optimize

...after which it can be used in code:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    => optionsBuilder
        .UseModel(MyCompiledModels.BlogsContextModel.Instance)
        .UseSqlServer(...)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1