using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; namespace APT.Infrastructure.EF { public class SimpleEntityScaner : IEntityScaner where TDbContext : DbContext { private static List dbProperties; private static object locker = new object(); public IList ScanEntityTypes() { if (dbProperties == null) { lock (locker) { if (dbProperties == null) { var contextType = typeof(TDbContext); var properties = contextType.GetProperties(); var entityProperties = properties.Where(r => IsDbSet(r)).ToList(); dbProperties = new List(); foreach (var property in entityProperties) { dbProperties.Add(new DbsetProperty { PropertyName = property.Name, PropertyType = property.PropertyType.GetGenericArguments()[0] }); } } } } return dbProperties; } bool IsDbSet(PropertyInfo property) { if (property.CanRead && property.PropertyType.IsGenericType && typeof(DbSet<>).GetGenericTypeDefinition().Equals(property.PropertyType.GetGenericTypeDefinition())) { return true; } return false; } } }