@@ -17,32 +17,50 @@ public static class UmbracoEFCoreServiceCollectionExtensions
17
17
/// <summary>
18
18
/// Adds a EFCore DbContext with all the services needed to integrate with Umbraco scopes.
19
19
/// </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 )
25
33
where T : DbContext
26
34
{
27
- return AddUmbracoDbContext < T > ( services , ( IServiceProvider _ , DbContextOptionsBuilder options ) =>
35
+ return AddUmbracoDbContext < T > ( services , ( IServiceProvider provider , DbContextOptionsBuilder optionsBuilder , string ? providerName , string ? connectionString ) =>
28
36
{
29
- optionsAction ? . Invoke ( options ) ;
37
+ ConnectionStrings connectionStrings = GetConnectionStringAndProviderName ( provider ) ;
38
+ optionsAction ? . Invoke ( optionsBuilder , connectionStrings . ConnectionString , connectionStrings . ProviderName , provider ) ;
30
39
} ) ;
31
40
}
32
41
33
42
/// <summary>
34
43
/// Adds a EFCore DbContext with all the services needed to integrate with Umbraco scopes.
35
44
/// </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 )
41
58
where T : DbContext
42
59
{
43
- optionsAction ??= ( sp , options ) => { } ;
60
+ optionsAction ??= ( sp , optionsBuilder , connectionString , providerName ) => { } ;
61
+
44
62
45
- services . AddPooledDbContextFactory < T > ( optionsAction ) ;
63
+ services . AddPooledDbContextFactory < T > ( ( provider , optionsBuilder ) => SetupDbContext ( optionsAction , provider , optionsBuilder ) ) ;
46
64
services . AddTransient ( services => services . GetRequiredService < IDbContextFactory < T > > ( ) . CreateDbContext ( ) ) ;
47
65
48
66
services . AddUnique < IAmbientEFCoreScopeStack < T > , AmbientEFCoreScopeStack < T > > ( ) ;
@@ -110,4 +128,25 @@ public static void UseUmbracoDatabaseProvider(this DbContextOptionsBuilder build
110
128
111
129
builder . UseDatabaseProvider ( connectionStrings . ProviderName , connectionStrings . ConnectionString ) ;
112
130
}
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
+ }
113
152
}
0 commit comments