This works, although, I'm not really interested in the first one.
Customer foundCustomer = context.Set<Customer>().Include(e => e.Orders.Select ( d => d.OrderLines)).FirstOrDefault();
This works, and I find the match I'm working for.
string custKey = "VINET";
Customer foundCustomer = context.Set<Customer>().Include(e => e.Orders.Select(d => d.OrderLines)).Where(c => c.CustomerID.Equals(custKey, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
This works, and reads the db as I want, but does not have the includes I want.
Customer foundCustomer = context.Set<Customer>().Find(custKey);
This does not work....but is what I'm actually after
Customer foundCustomer = context.Set<Customer>().Include(e => e.Orders.Select(d => d.OrderLines)).Find(custKey);
Is there any way to combine Include()
with Find()
?
Here is the Context. The typical Northwind Customer/Order(s)/OrderDetails scenario.
public partial class WindyContext : DbContext
{
static WindyContext()
{
//Database.SetInitializer<WindyContext>(null);
}
public WindyContext()
: base("Name=NorthwindContext")
{
}
public DbSet<Customer> Customers { get; set; }
public DbSet<OrderLine> DbSetOrderLines { get; set; }
public DbSet<Order> Orders { get; set; }
public DbSet<Product> Products { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new CustomerMap());
modelBuilder.Configurations.Add(new OrderLineMap());
modelBuilder.Configurations.Add(new OrderMap());
modelBuilder.Configurations.Add(new ProductMap());
}
}
Entity Framework Classic ThenIncludeThe ThenInclude method moves the chaining level to the property included. It allows us to include related objects from the next level. ThenInclude is a syntactic sugar method to make it easier and clearer to include multiple related objects.
Entity Framework supports three ways to load related data - eager loading, lazy loading and explicit loading. The techniques shown in this topic apply equally to models created with Code First and the EF Designer.
Eager loading is the process whereby a query for one type of entity also loads related entities as part of the query. Eager loading is achieved by use of the Include method.
Entity Framework (EF) Core is a lightweight, extensible, open source and cross-platform version of the popular Entity Framework data access technology. EF Core can serve as an object-relational mapper (O/RM), which: Enables . NET developers to work with a database using . NET objects.
Nope, no such luck. I don't know why the Find()
method doesn't have an overload that accepts a list of eager-load paths, but it might be because it tries to be efficient by first looking at the entities that have already been loaded into the context.
You best bet is the second example, but you can clean it up a little by putting the condition directly in the FirstOrDefault()
call:
Customer foundCustomer = context.Set<Customer>()
.Include(e => e.Orders.Select(d => d.OrderLines))
.FirstOrDefault(c => c.CustomerID.Equals(custKey, StringComparison.OrdinalIgnoreCase));
You also might be able to use ==
instead of Equals()
if your database/column collation is already set to case-insensitive.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With