diff --git a/src/DotNetEd.CoreAdmin/CoreAdminConfigurationExtensions.cs b/src/DotNetEd.CoreAdmin/CoreAdminConfigurationExtensions.cs index 9fddd65..df6bb31 100644 --- a/src/DotNetEd.CoreAdmin/CoreAdminConfigurationExtensions.cs +++ b/src/DotNetEd.CoreAdmin/CoreAdminConfigurationExtensions.cs @@ -50,7 +50,7 @@ public static void AddCoreAdmin(this IServiceCollection services, CoreAdminOptio public static void AddCoreAdmin(this IServiceCollection services, params string[] restrictToRoles) { - + var coreAdminOptions = new CoreAdminOptions(); FindDbContexts(services, coreAdminOptions); @@ -59,7 +59,7 @@ public static void AddCoreAdmin(this IServiceCollection services, params string[ { coreAdminOptions.RestrictToRoles = restrictToRoles; } - + services.AddSingleton(coreAdminOptions); AddLocalisation(services); @@ -116,29 +116,26 @@ private static void FindDbContexts(IServiceCollection services, CoreAdminOptions } var discoveredServices = new List(); - foreach (var service in services.ToList()) + + var dbContextImplementations = services + .Where(x => x.Lifetime is ServiceLifetime.Scoped && x.ServiceType.IsSubclassOf(typeof(DbContext))) + .ToList(); + + foreach (var dbContextImplementation in dbContextImplementations) { - if (service.ImplementationType == null) - continue; - if (service.ImplementationType.IsSubclassOf(typeof(DbContext)) && - !discoveredServices.Any(x => x.DbContextType == service.ImplementationType)) + foreach (var dbSetProperty in dbContextImplementation.ServiceType.GetProperties()) { - foreach (var dbSetProperty in service.ImplementationType.GetProperties()) + // looking for DbSet + if (dbSetProperty.PropertyType.IsGenericType && dbSetProperty.PropertyType.Name.StartsWith("DbSet")) { - // looking for DbSet - if (dbSetProperty.PropertyType.IsGenericType && dbSetProperty.PropertyType.Name.StartsWith("DbSet")) + if (!options.IgnoreEntityTypes.Contains(dbSetProperty.PropertyType.GenericTypeArguments.First())) { - if (!options.IgnoreEntityTypes.Contains(dbSetProperty.PropertyType.GenericTypeArguments.First())) - { - discoveredServices.Add(new DiscoveredDbSetEntityType() { - DbContextType = service.ImplementationType, - DbSetType = dbSetProperty.PropertyType, - UnderlyingType = dbSetProperty.PropertyType.GenericTypeArguments.First(), Name = dbSetProperty.Name }); - } + discoveredServices.Add(new DiscoveredDbSetEntityType { + DbContextType = dbContextImplementation.ServiceType, + DbSetType = dbSetProperty.PropertyType, + UnderlyingType = dbSetProperty.PropertyType.GenericTypeArguments.First(), Name = dbSetProperty.Name }); } } - - } }