Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
items.Count()Не затянули ли вы всю таблицу чтобы подсчитать количество записей?
Ну в общем то что заметил, разве что сложные отчеты это не к EF :).
Визуализировать схему базы
Я сделал подобный велосипед
Предупреждение EF1001 Microsoft.EntityFrameworkCore.Scaffolding.Internal.IScaffoldingModelFactory is an internal API that supports the Entity Framework Core infrastructure and not subject to the same compatibility standards as public APIs. It may be changed or removed without notice in any release.
— not everything about the model is presented in the database schema. For example inheritance hierarchies, owned types and table splitting will not be reverse-engineered
— also, EF Core documentation claims, that there are some column types that will not be included in the model
— nullable types will not be mapped as nullable. For example, string columns that can be null, will not be scaffolded as string? type.
public IEnumerable<string> GetTableNames()
{
DbContext context = contextFactory.CreateContext();
return context.Model.GetEntityTypes.Select(e => e.Name);
}
public TableInfo GetTableInfo(string entityName)
{
var entityType = GetEntityType(entityName);
var properties = entityType.GetProperties();
var efProperties = entityType.GetServiceProperties();
var allProperties = entityType.ClrType
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(p => !efProperties.Any(ep => ep.Name == p.Name));
return new TableInfo
{
EntityType = entityType,
Properties = properties,
AllProperties = allProperties,
NavProperties = allProperties.Where(p => !properties.Any(ep => ep.Name == p.Name))
};
}
public class TableInfo
{
public IEntityType EntityType { get; set; }
public IEnumerable<PropertyInfo> AllProperties { get; set; }
public IEnumerable<PropertyInfo> NavProperties { get; set; }
public IEnumerable<IProperty> Properties { get; set; }
}
Потом генерирую код запроса в виде строки, компилирую и выполняю его динамически с помощью Roslyn.
Для упрощения работы с результатами запроса использую Z.EntityFramework.Plus.EFCore.
Roslyn & EF Core: конструируем DbContext в runtime