Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the addition of the ServiceLifetime.Scoped check here an important change? It's unclear how that check is related to the core purpose of this PR (supporting AddDbContextPool()).

.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 {
Comment on lines +124 to +133
Copy link

@knumat knumat Jan 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this PR removes the check for duplicated tables in discoveredSevices (see #10 and #11). Would it make sense to add something like the following?

if (discoveredServices.Any(x => x.DbContextType == dbContextImplementation.ServiceType)))
    continue;

DbContextType = dbContextImplementation.ServiceType,
DbSetType = dbSetProperty.PropertyType,
UnderlyingType = dbSetProperty.PropertyType.GenericTypeArguments.First(), Name = dbSetProperty.Name });
}
}


}
}

Expand Down