Skip to content

Commit

Permalink
Add support for DbContextPool
Browse files Browse the repository at this point in the history
  • Loading branch information
iwedaz committed Dec 31, 2023
1 parent 704dfb1 commit 7a5d15e
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions src/DotNetEd.CoreAdmin/CoreAdminConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -59,7 +59,7 @@ public static void AddCoreAdmin(this IServiceCollection services, params string[
{
coreAdminOptions.RestrictToRoles = restrictToRoles;
}

services.AddSingleton(coreAdminOptions);

AddLocalisation(services);
Expand Down Expand Up @@ -116,29 +116,26 @@ private static void FindDbContexts(IServiceCollection services, CoreAdminOptions
}

var discoveredServices = new List<DiscoveredDbSetEntityType>();
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<Entity>
if (dbSetProperty.PropertyType.IsGenericType && dbSetProperty.PropertyType.Name.StartsWith("DbSet"))
{
// looking for DbSet<Entity>
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 });
}
}


}
}

Expand Down

0 comments on commit 7a5d15e

Please sign in to comment.