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();
}
}