22using LinkDotNet . Blog . Infrastructure . Persistence ;
33using LinkDotNet . Blog . Infrastructure . Persistence . Sql ;
44using Microsoft . EntityFrameworkCore ;
5- using Microsoft . EntityFrameworkCore . Infrastructure ;
65using Microsoft . Extensions . DependencyInjection ;
76
87namespace LinkDotNet . Blog . Web . RegistrationExtensions ;
@@ -13,33 +12,30 @@ public static void UseSqlAsStorageProvider(this IServiceCollection services)
1312 {
1413 services . AssertNotAlreadyRegistered ( typeof ( IRepository < > ) ) ;
1514
16- services . AddTransient ( s =>
15+ services . AddDbContextFactory < BlogDbContext > (
16+ ( s , builder ) =>
1717 {
1818 var configuration = s . GetService < AppConfiguration > ( ) ?? throw new NullReferenceException ( nameof ( AppConfiguration ) ) ;
1919 var connectionString = configuration . ConnectionString ;
20- var dbOptions = new DbContextOptionsBuilder < BlogDbContext > ( )
21- . UseSqlServer ( connectionString , options => options . EnableRetryOnFailure ( 3 , TimeSpan . FromSeconds ( 30 ) , null ) )
22- . Options ;
20+ builder . UseSqlServer ( connectionString ) ;
21+ } ,
22+ ServiceLifetime . Transient ) ;
2323
24- return new PooledDbContextFactory < BlogDbContext > ( dbOptions ) . CreateDbContext ( ) ;
25- } ) ;
2624 services . AddScoped ( typeof ( IRepository < > ) , typeof ( Repository < > ) ) ;
2725 }
2826
2927 public static void UseSqliteAsStorageProvider ( this IServiceCollection services )
3028 {
3129 services . AssertNotAlreadyRegistered ( typeof ( IRepository < > ) ) ;
3230
33- services . AddTransient ( s =>
31+ services . AddDbContextFactory < BlogDbContext > (
32+ ( s , builder ) =>
3433 {
3534 var configuration = s . GetService < AppConfiguration > ( ) ?? throw new NullReferenceException ( nameof ( AppConfiguration ) ) ;
3635 var connectionString = configuration . ConnectionString ;
37- var dbOptions = new DbContextOptionsBuilder < BlogDbContext > ( )
38- . UseSqlite ( connectionString )
39- . Options ;
40-
41- return new PooledDbContextFactory < BlogDbContext > ( dbOptions ) . CreateDbContext ( ) ;
42- } ) ;
36+ builder . UseSqlite ( connectionString ) ;
37+ } ,
38+ ServiceLifetime . Transient ) ;
4339 services . AddScoped ( typeof ( IRepository < > ) , typeof ( Repository < > ) ) ;
4440 }
45- }
41+ }
0 commit comments