1- using BookLibrary . Application . Infrastructure ;
1+ using BookLibrary . Application . Infrastructure ;
22using BookLibrary . Infrastructure . ValueConverters ;
33using EntityFramework . Exceptions . PostgreSQL ;
44using Microsoft . EntityFrameworkCore ;
88using Microsoft . Extensions . Hosting ;
99using Microsoft . Extensions . Options ;
1010using Npgsql ;
11+ using Npgsql . EntityFrameworkCore . PostgreSQL . Infrastructure ;
1112
1213namespace BookLibrary . Infrastructure . EntityFramework ;
1314
@@ -20,8 +21,6 @@ internal static class ServiceCollectionExtensions
2021 /// <returns>Service registrator.</returns>
2122 public static IServiceCollection AddEntityFramework ( this IServiceCollection services )
2223 {
23- NpgsqlDataSource ? npgsqlDataSource = null ; // should be singleton
24-
2524 services
2625 . AddTransient < IConfigureOptions < ApplicationContextSettings > , ConfigureApplicationContextSettings > ( )
2726 . AddOptions < ApplicationContextSettings > ( )
@@ -44,24 +43,43 @@ public static IServiceCollection AddEntityFramework(this IServiceCollection serv
4443 . EnableSensitiveDataLogging ( ) ;
4544 }
4645
47- options . ConfigureWarnings ( w => w . Ignore ( CoreEventId . FirstWithoutOrderByAndFilterWarning ) ) ;
48-
49- npgsqlDataSource ??= new NpgsqlDataSourceBuilder ( config . Value . ConnectionString )
50- . EnableDynamicJson ( )
51- . BuildMultiHost ( )
52- . WithTargetSession ( TargetSessionAttributes . Primary ) ;
53-
5446 options
47+ . ConfigureWarnings ( w => w . Ignore ( CoreEventId . FirstWithoutOrderByAndFilterWarning ) )
5548 . ReplaceService < IValueConverterSelector , ValueObjectsConverterSelectorByConvention > ( )
5649 . UseQueryTrackingBehavior ( QueryTrackingBehavior . NoTracking )
57- . UseNpgsql ( npgsqlDataSource , builder =>
58- {
59- builder . UseQuerySplittingBehavior ( QuerySplittingBehavior . SplitQuery ) ;
60- builder . MigrationsAssembly ( typeof ( ApplicationContext ) . Assembly . GetName ( ) . Name ) ;
61- builder . MigrationsHistoryTable ( "__EFMigrationHistory" , ApplicationContext . DefaultScheme ) ;
62- } )
6350 . UseSnakeCaseNamingConvention ( )
6451 . UseExceptionProcessor ( ) ;
52+
53+ var accessor = sp . GetService < NpgsqlDataSourceAccessor > ( ) ;
54+
55+ if ( accessor is not null )
56+ {
57+ options . UseNpgsql ( accessor . NpgsqlDataSource , b =>
58+ {
59+ b . ConfigureNpgsql ( ) ;
60+ } ) ;
61+ }
62+ else
63+ {
64+ options . UseNpgsql ( config . Value . ConnectionString , b =>
65+ {
66+ b . ConfigureNpgsql ( ) ;
67+ b . ConfigureDataSource ( ds => ds
68+ . EnableDynamicJson ( )
69+ . BuildMultiHost ( )
70+ . WithTargetSession ( TargetSessionAttributes . Primary )
71+ ) ;
72+ } ) ;
73+ }
6574 } ) ;
6675 }
67- }
76+
77+ private static void ConfigureNpgsql ( this NpgsqlDbContextOptionsBuilder b )
78+ {
79+ b . UseQuerySplittingBehavior ( QuerySplittingBehavior . SplitQuery ) ;
80+ b . MigrationsAssembly ( typeof ( ApplicationContext ) . Assembly . GetName ( ) . Name ) ;
81+ b . MigrationsHistoryTable ( "__EFMigrationHistory" , ApplicationContext . DefaultScheme ) ;
82+ }
83+ }
84+
85+ public sealed record NpgsqlDataSourceAccessor ( NpgsqlDataSource NpgsqlDataSource ) ;
0 commit comments