Skip to content
This repository was archived by the owner on Apr 17, 2025. It is now read-only.

Commit 14fb09e

Browse files
committed
Aggiunto metodo AddDbContextUseSQLServer
1 parent 486bb0d commit 14fb09e

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/NET6CustomLibrary/Extensions/DependencyInjection.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,28 @@ public static IServiceCollection AddDbContextUsePostgres<TDbContext>(this IServi
144144
return services;
145145
}
146146

147+
public static IServiceCollection AddDbContextUseSQLServer<TDbContext>(this IServiceCollection services, string connectionString, int retryOnFailure) where TDbContext : DbContext
148+
{
149+
services.AddDbContextPool<TDbContext>(optionBuilder =>
150+
{
151+
if (retryOnFailure > 0)
152+
{
153+
optionBuilder.UseSqlServer(connectionString, options =>
154+
{
155+
// Abilito il connection resiliency (Provider di SQL Server è soggetto a errori transienti)
156+
// Info su: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
157+
options.EnableRetryOnFailure(retryOnFailure);
158+
});
159+
}
160+
else
161+
{
162+
optionBuilder.UseSqlServer(connectionString);
163+
}
164+
});
165+
166+
return services;
167+
}
168+
147169
public static IServiceCollection AddDbContextUseSQLite<TDbContext>(this IServiceCollection services, string connectionString) where TDbContext : DbContext
148170
{
149171
services.AddDbContextPool<TDbContext>(optionsBuilder =>
@@ -156,7 +178,6 @@ public static IServiceCollection AddDbContextUseSQLite<TDbContext>(this IService
156178

157179
return services;
158180
}
159-
160181
#endregion
161182

162183
#region "HEALTH CHECKS"

0 commit comments

Comments
 (0)