Skip to content

Commit 94be617

Browse files
committed
Use DbFactory instead of direct dbcontext to not run into exceptions
1 parent 182fc6f commit 94be617

File tree

2 files changed

+12
-16
lines changed

2 files changed

+12
-16
lines changed

src/LinkDotNet.Blog.Web/Features/Home/Components/AccessControl.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<li><h6 class="dropdown-header">Others</h6></li>
1717
<li><a class="dropdown-item" href="Sitemap">Sitemap</a></li>
1818
<li><hr class="dropdown-divider"></li>
19-
<li><a class="dropdown-item" target="_blank" href="https://github.com/linkdotnet/Blog/releases">Version 3.4</a></li>
19+
<li><a class="dropdown-item" target="_blank" href="https://github.com/linkdotnet/Blog/releases">Version 3.5</a></li>
2020
</ul>
2121
</li>
2222
<li class="nav-item"><a class="nav-link" href="logout?redirectUri=@CurrentUri"><i class="fa-solid fa-lock"></i> Log out</a></li>

src/LinkDotNet.Blog.Web/RegistrationExtensions/SqlRegistrationExtensions.cs

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

87
namespace 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

Comments
 (0)