2
2
using LinkDotNet . Blog . Infrastructure . Persistence ;
3
3
using LinkDotNet . Blog . Infrastructure . Persistence . Sql ;
4
4
using Microsoft . EntityFrameworkCore ;
5
- using Microsoft . EntityFrameworkCore . Infrastructure ;
6
5
using Microsoft . Extensions . DependencyInjection ;
7
6
8
7
namespace LinkDotNet . Blog . Web . RegistrationExtensions ;
@@ -13,33 +12,30 @@ public static void UseSqlAsStorageProvider(this IServiceCollection services)
13
12
{
14
13
services . AssertNotAlreadyRegistered ( typeof ( IRepository < > ) ) ;
15
14
16
- services . AddTransient ( s =>
15
+ services . AddDbContextFactory < BlogDbContext > (
16
+ ( s , builder ) =>
17
17
{
18
18
var configuration = s . GetService < AppConfiguration > ( ) ?? throw new NullReferenceException ( nameof ( AppConfiguration ) ) ;
19
19
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 ) ;
23
23
24
- return new PooledDbContextFactory < BlogDbContext > ( dbOptions ) . CreateDbContext ( ) ;
25
- } ) ;
26
24
services . AddScoped ( typeof ( IRepository < > ) , typeof ( Repository < > ) ) ;
27
25
}
28
26
29
27
public static void UseSqliteAsStorageProvider ( this IServiceCollection services )
30
28
{
31
29
services . AssertNotAlreadyRegistered ( typeof ( IRepository < > ) ) ;
32
30
33
- services . AddTransient ( s =>
31
+ services . AddDbContextFactory < BlogDbContext > (
32
+ ( s , builder ) =>
34
33
{
35
34
var configuration = s . GetService < AppConfiguration > ( ) ?? throw new NullReferenceException ( nameof ( AppConfiguration ) ) ;
36
35
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 ) ;
43
39
services . AddScoped ( typeof ( IRepository < > ) , typeof ( Repository < > ) ) ;
44
40
}
45
- }
41
+ }
0 commit comments