Find and enable with EF 6

It works though, I'm not very interested in the first one.

Customer foundCustomer = context.Set<Customer>().Include(e => e.Orders.Select ( d => d.OrderLines)).FirstOrDefault(); 

This works, and I find a match I'm working on.

 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 db as I want, but does not have the inclusions that I want.

 Customer foundCustomer = context.Set<Customer>().Find(custKey); 

This does not work ... but this is what I actually after

 Customer foundCustomer = context.Set<Customer>().Include(e => e.Orders.Select(d => d.OrderLines)).Find(custKey); 

Is there a way to combine Include() with Find() ?

Here is the context. Typical Northwind Customer / Order (s) / OrderDetails script.

 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()); } } 
+7
entity-framework
source share
2 answers

No, there is no such luck. I do not know why the Find() method does not have an overload that accepts a list of paths with a high load, but maybe because it tries to be efficient by first looking at objects that have already been loaded into the context.

The best option is the second example, but you can clean it up a bit by setting 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 can also use == instead of Equals() if case insensitivity is already set in the database / column setup.

+7
source share

I do not believe that you can use "Enable" when using "Find." If you want to explicitly load other objects, you can try the following:

 var customer = context.Customers.Include(x => x.Orders) .Include("Orders.OrderLines") .SingleOrDefault(x => x.CustomerID == custKey); 
+3
source share

All Articles