Skip to content

Commit b9256b2

Browse files
committed
Use PooledDbConnecton in EF Core
1 parent 3957bc2 commit b9256b2

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

LinkDotNet.Blog.Web/RegistrationExtensions/SqlRegistrationExtensions.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using LinkDotNet.Blog.Infrastructure.Persistence;
33
using LinkDotNet.Blog.Infrastructure.Persistence.Sql;
44
using Microsoft.EntityFrameworkCore;
5+
using Microsoft.EntityFrameworkCore.Infrastructure;
56
using Microsoft.Extensions.DependencyInjection;
67

78
namespace LinkDotNet.Blog.Web.RegistrationExtensions;
@@ -16,11 +17,11 @@ public static void UseSqlAsStorageProvider(this IServiceCollection services)
1617
{
1718
var configuration = s.GetService<AppConfiguration>() ?? throw new NullReferenceException(nameof(AppConfiguration));
1819
var connectionString = configuration.ConnectionString;
19-
var dbOptions = new DbContextOptionsBuilder()
20+
var dbOptions = new DbContextOptionsBuilder<BlogDbContext>()
2021
.UseSqlServer(connectionString, options => options.EnableRetryOnFailure(3, TimeSpan.FromSeconds(30), null))
2122
.Options;
2223

23-
return new BlogDbContext(dbOptions);
24+
return new PooledDbContextFactory<BlogDbContext>(dbOptions).CreateDbContext();
2425
});
2526
services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
2627
}
@@ -33,11 +34,11 @@ public static void UseSqliteAsStorageProvider(this IServiceCollection services)
3334
{
3435
var configuration = s.GetService<AppConfiguration>() ?? throw new NullReferenceException(nameof(AppConfiguration));
3536
var connectionString = configuration.ConnectionString;
36-
var dbOptions = new DbContextOptionsBuilder()
37+
var dbOptions = new DbContextOptionsBuilder<BlogDbContext>()
3738
.UseSqlite(connectionString)
3839
.Options;
3940

40-
return new BlogDbContext(dbOptions);
41+
return new PooledDbContextFactory<BlogDbContext>(dbOptions).CreateDbContext();
4142
});
4243
services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
4344
}

0 commit comments

Comments
 (0)