Using Entity Framework Core query with .Include() to eager load child data is causing SQL Server to time out

entity-framework-core sql-server

Question

I have an Entity Framework Core 2.1 project with a parent/child entity relationship . When I execute a query against the parent table, the results are returned nearly immediately. When I attempt to Context.Parents.Include(parent => parent.Children), the query appears to execute properly on the server (by viewing the SQL Server Profiler output), but the application code gets a timeout exception after 20 seconds. This is a small data set (~20 records in parent table and ~100 records in child table), so there should not be any real performance issue. Does anyone have any idea what I'm doing wrong here? I suspect that I have misconfigured my context, but I'm not certain.

EDIT
Here are my classes:

public partial class A
{
    public int Id { get; set; }
    public bool Active { get; set; }
    public ICollection<B> BItems { get; set; }
}

public partial class B
{
    public int Id { get; set; }
    public int AId { get; set; }
    public A AItem { get; set; }
}

public partial class Context
{
    public virtual DbSet<A> A { get; set; }
    public virtual DbSet<B> B { get; set; }
    protected override OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<B>(entity =>
        {
            // Other field setup, unrelated to relationship...

            entity.HasOne(b => b.A)
                .WithMany(a => a.BItems)
                .HasForeignKey(b => b.AId)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK_ConstraintName");
        });

        modelBuilder.Entity<A>(entity =>
        {
             // Other field setup, unrelated to relationship
        });
    }
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            optionsBuilder.UseSqlServer(...connection string from configuration goes here...);
        }
    }
}

My usage code looks like this:

var results = await context.A
    .Include(a => a.BItems)
    .Where(a => a.Active)
    .ToListAsync();

I'm using EF Core 2.1 against SQL Server 2016. I see 2 queries executed in SQL Server Profiler; one to query for the A records and one to query for the B records that is INNER JOINed against the A table.

1
2
8/3/2018 1:08:14 PM

Popular Answer

This issue resolved itself over time, and I'm not entirely sure what did it. Part of my suspicious was hardware related, as we had other local issues on our LAN that manifested themselves in other ways while working on this project.

0
12/10/2018 3:36:49 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow