简体   繁体   中英

Generic method to retrieve DbSet <T> from DbContext and Using Include() method c#

I made this function that retrieves all the data.

public static List<Volunteer> GetAllVolunteers()
{
    using (VolunteerPlacementSystemDBEntities1 db = new VolunteerPlacementSystemDBEntities1())
    {
        return db.Volunteers.**Include(v => v.roleForVolunteers).Include(v => v.VolunteerOffers)
                         .Include("VolunteerOffers.daysForAVolunteers")**.ToList();
    }
}

And I have this generic function that retrieves DBSet T.

public static List<T> GetDbSet<T>() where T : class
{
    using (VolunteerPlacementSystemDBEntities1 db = new VolunteerPlacementSystemDBEntities1())
    {
        return db.GetDbSet<T>().ToList();
    }
}

Can I make a generic function that will retrieve all the data that connect to one DBSet T?

Thanks in advance.

Get DbSet for the T Entity Class Type from context as DbSet<T> or IQueryable<T> .

On this IQueryable<T> object can perform multiple operations like Where() , Include() , orderBy() etc with help of namespace System.Linq .

  • With Include() method, pass on required included properties.

  • For Parallel processing use TPL from System.Threading.Tasks

Try this:

public static List<T> GetDbSet<T>(string[] includeProperties) where T : class
{
    using (VolunteerPlacementSystemDBEntities1 db = new VolunteerPlacementSystemDBEntities1())
    {
        IQueryable<T> query = db.Set<T>();

        Parallel.ForEach(inlcudeProperties, includeProperty => {
           query.Include(includeProperty);
        });

        return query.ToList();
    }
}

Some month ago i try to do somthing like that, but i have tableName. I leave you some detail, that you can adapt

You extract form context all your table and select only the ClrType corrispond with your T type.

var selectedTable = contextDb.Model
            .GetEntityTypes()
            .Select(x => new TableSpecification // Custom class
            {
                TableName = x.GetTableName(),
                ClrType = x.ClrType,
                JoinTable = x.GetForeignKeys(),
                JoinTableProp = x.GetForeignKeyProperties(),
                NavigationProp = x.GetNavigations(),
                ColumnsTable = null
            })
           .Where(// where ClrType)
           .Distinct()
           .FirstOrDefault();

This is my custom class

using Microsoft.EntityFrameworkCore.Metadata;

using System;
using System.Collections.Generic;

namespace Seac.CRM.DbInfrastructure.Model.Context;

public class TableSpecification
{
    public string TableName { get; init; }
    public Type ClrType { get; init; }
    public IEnumerable<IForeignKey> JoinTable { get; init; }
    public IEnumerable<IProperty> JoinTableProp { get; init; }
    public IEnumerable<INavigation> NavigationProp { get; init; }
    public IEnumerable<IProperty> ColumnsTable { get; init; }
}

then you can extract all your reference property

var joinLookup = selectedTable.JoinTable.GetLookupForeignKey();

foreach (var lookup in joinLookup)
   queryable = queryable.Include(lookup.PrincipalEntityType.Name.Split(".").Last());

I wish that can help you.

*** EDIT

If you need a custom include of T you can use IIncludableQueryable<T, object>>

public static List<T> GetDbSet<T>(IIncludableQueryable<T, object>> include = null) where T : class
{
    using (VolunteerPlacementSystemDBEntities1 db = new VolunteerPlacementSystemDBEntities1())
    {
        IQueryable<T> query = db.Set<T>();

        if (includesFunc is not null)
            query = includesFunc(query);

        return query.ToList();
    }
}

Thanks to everyone who tried to help me. This is the solution that worked for me.

  public static List<T> GetDbSetWithIncludes<T>(string[] includes) where T : class
    {
        using (VolunteerPlacementSystemDBEntities1 db = new VolunteerPlacementSystemDBEntities1())
        {
            db.Configuration.LazyLoadingEnabled = false;
            return IncludeMultiple<T>(db.GetDbSet<T>(), includes).ToList();
        }
    }


 public static IQueryable<T> IncludeMultiple<T>(IQueryable<T> query, string[] includes)where T : class
        {
            if (includes != null)
            {
                query = includes.Aggregate(query, (current, include) =>
                current.Include(include));
            }
            return query;
        }

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