简体   繁体   中英

EF Core - Include related entities using a generic method

previously, back in EF Frameowrk days, the following method sufficed when needing the related entities to an entity.

private IQueryable<T> EntitySet(params Expression<Func<T, object>>[] includes)
    {
        var set = Context.Set<T>().Where(x => !x.IsDeleted);
        if (includes != null)
        {
            foreach (var include in includes)
            {
                set = set.Include(include);
            }                
        }
        return set;
    }

I'd call this method with something like EntitySet(x => x.People, x => x.People.Meetings) . However, ever since moving to EF core, I can no longer get the related data because I need to explicitly use Include(x.People).ThenInclude(p.Meegings) .

Is what I'm trying to do even possible in EF core? Or were there so many changes in gather related data so that the above method is no longer applicable?

It almost seems like you are attempting to add a filter to many queries of the same DbSet. Have you looked at Query Filters before in EF Core? It seems very similar to what you are trying to do. If you need to have a query should get all records instead of just active records, there is an option to ignore query filters at a by query basis.

https://docs.microsoft.com/en-us/ef/core/querying/filters

Edit: If you are looking to dynamically specify your includes still. You can do so with Strings in EF Core the same as was done in EF. However, the ability to use a lambda to access child objects may have gotten lost in conversion. https://entityframeworkcore.com/knowledge-base/38083198/add-include-expressions-dynamically

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM