1

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.

Jackal
  • 2,979
  • 3
  • 20
  • 59
  • 1
    What kind of CRUD operation you do on the first call? Show us the minimum reproductible sample please. – CodeNotFound Jul 26 '19 at 09:30
  • 1
    Probably a dupe of https://stackoverflow.com/questions/49353909/ef-core-2-1-slow-startup , https://stackoverflow.com/questions/52423965/entity-framework-core-slow-first-time-loading , https://stackoverflow.com/questions/30423838/entity-framework-very-slow-to-load-for-first-time-after-every-compilation or https://stackoverflow.com/questions/46683370/startup-first-query-extremely-slow . What happens when you run under `release` mode, outside of the debugger? – spender Jul 26 '19 at 10:02
  • It's slightly faster by 4-5 seconds out of 10-12. I misunderstood the question. This is on Production. Locally, on release it took 10s+, no debugging – Jackal Jul 26 '19 at 10:05

1 Answers1

2

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(...)
Gert Arnold
  • 100,019
  • 29
  • 193
  • 278