@@ -17,32 +17,50 @@ public static class UmbracoEFCoreServiceCollectionExtensions
1717 /// <summary>
1818 /// Adds a EFCore DbContext with all the services needed to integrate with Umbraco scopes.
1919 /// </summary>
20- /// <typeparam name="T"></typeparam>
21- /// <param name="services"></param>
22- /// <param name="optionsAction"></param>
23- /// <returns></returns>
24- public static IServiceCollection AddUmbracoDbContext < T > ( this IServiceCollection services , Action < DbContextOptionsBuilder > ? optionsAction = null )
20+ [ Obsolete ( "Please use the method overload that takes all parameters for the optionsAction. Scheduled for removal in Umbraco 18." ) ]
21+ public static IServiceCollection AddUmbracoDbContext < T > (
22+ this IServiceCollection services ,
23+ Action < DbContextOptionsBuilder > ? optionsAction = null )
24+ where T : DbContext
25+ => AddUmbracoDbContext < T > ( services , ( sp , optionsBuilder , connectionString , providerName ) => optionsAction ? . Invoke ( optionsBuilder ) ) ;
26+
27+ /// <summary>
28+ /// Adds a EFCore DbContext with all the services needed to integrate with Umbraco scopes.
29+ /// </summary>
30+ public static IServiceCollection AddUmbracoDbContext < T > (
31+ this IServiceCollection services ,
32+ Action < DbContextOptionsBuilder , string ? , string ? , IServiceProvider ? > ? optionsAction = null )
2533 where T : DbContext
2634 {
27- return AddUmbracoDbContext < T > ( services , ( IServiceProvider _ , DbContextOptionsBuilder options ) =>
35+ return AddUmbracoDbContext < T > ( services , ( IServiceProvider provider , DbContextOptionsBuilder optionsBuilder , string ? providerName , string ? connectionString ) =>
2836 {
29- optionsAction ? . Invoke ( options ) ;
37+ ConnectionStrings connectionStrings = GetConnectionStringAndProviderName ( provider ) ;
38+ optionsAction ? . Invoke ( optionsBuilder , connectionStrings . ConnectionString , connectionStrings . ProviderName , provider ) ;
3039 } ) ;
3140 }
3241
3342 /// <summary>
3443 /// Adds a EFCore DbContext with all the services needed to integrate with Umbraco scopes.
3544 /// </summary>
36- /// <typeparam name="T"></typeparam>
37- /// <param name="services"></param>
38- /// <param name="optionsAction"></param>
39- /// <returns></returns>
40- public static IServiceCollection AddUmbracoDbContext < T > ( this IServiceCollection services , Action < IServiceProvider , DbContextOptionsBuilder > ? optionsAction = null )
45+ [ Obsolete ( "Please use the method overload that takes all parameters for the optionsAction. Scheduled for removal in Umbraco 18." ) ]
46+ public static IServiceCollection AddUmbracoDbContext < T > (
47+ this IServiceCollection services ,
48+ Action < IServiceProvider , DbContextOptionsBuilder > ? optionsAction = null )
49+ where T : DbContext
50+ => AddUmbracoDbContext < T > ( services , ( sp , optionsBuilder , connectionString , providerName ) => optionsAction ? . Invoke ( sp , optionsBuilder ) ) ;
51+
52+ /// <summary>
53+ /// Adds a EFCore DbContext with all the services needed to integrate with Umbraco scopes.
54+ /// </summary>
55+ public static IServiceCollection AddUmbracoDbContext < T > (
56+ this IServiceCollection services ,
57+ Action < IServiceProvider , DbContextOptionsBuilder , string ? , string ? > ? optionsAction = null )
4158 where T : DbContext
4259 {
43- optionsAction ??= ( sp , options ) => { } ;
60+ optionsAction ??= ( sp , optionsBuilder , connectionString , providerName ) => { } ;
61+
4462
45- services . AddPooledDbContextFactory < T > ( optionsAction ) ;
63+ services . AddPooledDbContextFactory < T > ( ( provider , optionsBuilder ) => SetupDbContext ( optionsAction , provider , optionsBuilder ) ) ;
4664 services . AddTransient ( services => services . GetRequiredService < IDbContextFactory < T > > ( ) . CreateDbContext ( ) ) ;
4765
4866 services . AddUnique < IAmbientEFCoreScopeStack < T > , AmbientEFCoreScopeStack < T > > ( ) ;
@@ -110,4 +128,25 @@ public static void UseUmbracoDatabaseProvider(this DbContextOptionsBuilder build
110128
111129 builder . UseDatabaseProvider ( connectionStrings . ProviderName , connectionStrings . ConnectionString ) ;
112130 }
131+
132+ private static void SetupDbContext ( Action < IServiceProvider , DbContextOptionsBuilder , string ? , string ? > ? optionsAction , IServiceProvider provider , DbContextOptionsBuilder builder )
133+ {
134+ ConnectionStrings connectionStrings = GetConnectionStringAndProviderName ( provider ) ;
135+
136+ optionsAction ? . Invoke ( provider , builder , connectionStrings . ConnectionString , connectionStrings . ProviderName ) ;
137+ }
138+
139+ private static ConnectionStrings GetConnectionStringAndProviderName ( IServiceProvider serviceProvider )
140+ {
141+ ConnectionStrings connectionStrings = serviceProvider . GetRequiredService < IOptionsMonitor < ConnectionStrings > > ( ) . CurrentValue ;
142+
143+ // Replace data directory
144+ string ? dataDirectory = AppDomain . CurrentDomain . GetData ( Constants . System . DataDirectoryName ) ? . ToString ( ) ;
145+ if ( string . IsNullOrEmpty ( dataDirectory ) is false )
146+ {
147+ connectionStrings . ConnectionString = connectionStrings . ConnectionString ? . Replace ( Constants . System . DataDirectoryPlaceholder , dataDirectory ) ;
148+ }
149+
150+ return connectionStrings ;
151+ }
113152}
0 commit comments