How to avoid n+1 queries in EF Core 2.1?

asp.net-core-2.0 c# entity-framework-core sql-server

Question

I'm using EF Core 2.1 preview which was supposed to reduce N+1 queries problem. I'm trying to make query, that selects Forum Threads with authors of posts:

dbContext.ForumThreads
   .Include(t => t.Posts)
   .Take(n)
   .Select(t => new
   {
      t.Id,
      t.Title,
      PostAuhtors = t.Posts.Select(p => p.Author).Take(5)
   }).ToArray();

This produces n+1 queries: For each ForumThread it selects post authors

The schema is simple:

public class ForumThread 
{
   public Guid Id {get;set;}
   public string Title {get;set;}
   public ICollection<ForumPost> Posts {get;set;}
}

public class ForumPost 
{
  public Guid Id {get;set;}
  public string Author {get;set;}
  public string Content {get;set;}
}
1
5
3/19/2018 7:07:57 AM

Popular Answer

I think you can achieve that with less queries (only 2), making some of that behavior in memory. Does this code do what you want?

class Program
    {
        static void Main(string[] args)
        {
            using (var db = new SampleContext())
            {
                Console.ReadLine();
                var result = db.Threads
                    .Include(t => t.Posts)
                    .Take(10)
                    .Select(t => new
                    {
                        t.Id,
                        t.Title,
                        t.Posts
                        // Do this in memory  
                        //PostAuhtors = t.Posts.Select(p => p.Author).Take(5)
                    }).ToArray();

                Console.WriteLine($"» {result.Count()} Threads.");
                foreach (var thread in result)
                {
                    // HERE !!
                    var PostAuhtors = thread.Posts.Select(p => p.Author).Take(5);
                    Console.WriteLine($"» {thread.Title}:  {string.Join("; ", PostAuhtors)} authors");
                }
                Console.ReadLine();
            }
        }
    }

    public class SampleContext : DbContext
    {
        public static readonly LoggerFactory MyLoggerFactory = new LoggerFactory(new[] {
                new ConsoleLoggerProvider((category, level)
                    => category == DbLoggerCategory.Database.Command.Name
                       && level == LogLevel.Debug, true)
            });

        public DbSet<ForumThread> Threads { get; set; }
        public DbSet<ForumPost> Posts { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder  
                .EnableSensitiveDataLogging()
                .UseLoggerFactory(MyLoggerFactory)
                .UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=EFStart;Trusted_Connection=True;");
        }
    }

    public class ForumThread
    {
        public Guid Id { get; set; }
        public string Title { get; set; }
        public ICollection<ForumPost> Posts { get; set; }
    }

    public class ForumPost
    {
        public Guid Id { get; set; }
        public string Author { get; set; }
        public string Content { get; set; }
    }

This is the output: enter image description here

1
3/18/2018 9:57:55 AM


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