Вы можете переопределить метод Query
для DbContext.
Сначала создайте пользовательский метод запроса:
public static partial class CustomExtensions
{
public static IQueryable Query(this DbContext context, string entityName) =>
context.Query(context.Model.FindEntityType(entityName).ClrType);
static readonly MethodInfo SetMethod = typeof(DbContext).GetMethod(nameof(DbContext.Set));
public static IQueryable Query(this DbContext context, Type entityType) =>
(IQueryable)SetMethod.MakeGenericMethod(entityType).Invoke(context, null);
}
Затем необходимо сохранить table names and types
всех таблицы, участвующие в Dictionary
.
Dictionary<string, Type> TableTypeDictionary = new Dictionary<string, Type>()
{
{ "Teachers", typeof(Teachers) },//store the tables name and type.
{ "Students", typeof(Students) },
{ "Product", typeof(Product) }
//...
};
Последнее, используйте db для вызова метода Query:
var query = Db.Query(TableTypeDictionary[queryParams.Table]).Select($"new ({string.Join(",", queryParams.Columns)})", "T", StringComparison.OrdinalIgnoreCase);
Вы также можете ввести "Namespace.MyTable"
в метод Query. В настоящее время вам необходимо переписать метод Query в другой метод записи, см. this .