Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find and Include with EF 6

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());
    }
}
like image 771
granadaCoder Avatar asked Jan 17 '14 16:01

granadaCoder


People also ask

What is include and ThenInclude in Entity Framework?

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.

How you can load related entities in EF?

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.

What is Eager loading in EF?

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.

What is an entity in EF core?

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.


1 Answers

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.

like image 111
luksan Avatar answered Oct 02 '22 14:10

luksan