Skip to content

Add support for DbContextPool #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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)))
.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