Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include several references on the second level

Assume we have this model :

public class Tiers
{
    public List<Contact> Contacts { get; set; }
}

and

public class Contact
{
    public int Id { get; set; }
    public Tiers Tiers { get; set; }
    public Titre Titre { get; set; }
    public TypeContact TypeContact { get; set; }
    public Langue Langue { get; set; }
    public Fonction Fonction { get; set; }
    public Service Service { get; set; }
    public StatutMail StatutMail { get; set; }
}

With EF7 I would like to retrieve all data from the Tiers table, with data from the Contact table, from the Titre table, from the TypeContact table and so on ... with one single instruction. With Include/ThenInclude API I can write something like this :

_dbSet
     .Include(tiers => tiers.Contacts)
          .ThenInclude(contact => contact.Titre)
     .ToList();

But after Titre property, I can't include others references like TypeContact, Langue, Fonction ... Include method suggests a Tiers objects, and ThenInclude suggests a Titre object, but not a Contact object. How can I include all references from my list of Contact? Can we achieve this with one single instruction?

like image 676
Christophe Gigax Avatar asked Oct 08 '22 10:10

Christophe Gigax


People also ask

How do you use include and ThenInclude?

The difference is that Include will reference the table you are originally querying on regardless of where it is placed in the chain, while ThenInclude will reference the last table included. This means that you would not be able to include anything from your second table if you only used Include.

How do I use lazy loading in Entity Framework?

Lazy loading means delaying the loading of related data, until you specifically request for it. When using POCO entity types, lazy loading is achieved by creating instances of derived proxy types and then overriding virtual properties to add the loading hook.

What is Entity Framework 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.


2 Answers

For completeness' sake:

It is also possible to include nested properties directly via Include in case they are not collection properties like so:

_dbSet
    .Include(tier => tier.Contact.Titre)
    .Include(tier => tier.Contact.TypeContact)
    .Include(tier => tier.Contact.Langue);
like image 22
Risadinha Avatar answered Oct 10 '22 00:10

Risadinha


.ThenInclude() will chain off of either the last .ThenInclude() or the last .Include() (whichever is more recent) to pull in multiple levels. To include multiple siblings at the same level, just use another .Include() chain. Formatting the code right can drastically improve readability.

_dbSet
    .Include(tiers => tiers.Contacts).ThenInclude(contact => contact.Titre)
    .Include(tiers => tiers.Contacts).ThenInclude(contact => contact.TypeContact)
    .Include(tiers => tiers.Contacts).ThenInclude(contact => contact.Langue);
    // etc.
like image 254
bricelam Avatar answered Oct 10 '22 00:10

bricelam