Issues with Lazy Loading in Entity Framework Core 7 Leading to Multiple Queries Instead of Single Join
I'm working on a project and hit a roadblock. I've looked through the documentation and I'm still confused about This might be a silly question, but I'm facing a challenge with lazy loading in Entity Framework Core 7... I have two related entities, `Order` and `Customer`, where each order is associated with a customer. I expected that when I accessed the `Customer` navigation property from an `Order` instance, it would issue a single join query. However, I'm seeing multiple queries being executed instead, which is impacting performance. Here's a simplified version of my classes: ```csharp public class Order { public int Id { get; set; } public int CustomerId { get; set; } public virtual Customer Customer { get; set; } } public class Customer { public int Id { get; set; } public string Name { get; set; } public virtual ICollection<Order> Orders { get; set; } } ``` In my `DbContext`, I've enabled lazy loading like this: ```csharp public class ApplicationContext : DbContext { public DbSet<Order> Orders { get; set; } public DbSet<Customer> Customers { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Order>() .HasOne(o => o.Customer) .WithMany(c => c.Orders) .HasForeignKey(o => o.CustomerId); } } ``` When I retrieve an order from the database like this: ```csharp using (var context = new ApplicationContext()) { var order = context.Orders.FirstOrDefault(o => o.Id == 1); var customerName = order.Customer.Name; // Accessing the navigation property } ``` I get multiple queries being executed; the first retrieves the order, and the second retrieves the customer. I've verified that lazy loading is indeed enabled, but Iām unsure why this is happening. I've also tried eager loading with `.Include(o => o.Customer)` and it works fine, but I need lazy loading for specific cases where I don't want to load related entities unnecessarily. Is there a best practice or configuration I'm missing to ensure that accessing the lazy-loaded property results in a single SQL join instead of separate queries? Any insights would be greatly appreciated! My development environment is Windows. My development environment is Windows. Any help would be greatly appreciated! Thanks in advance! Thanks for taking the time to read this!