-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqlServerDbContextFactory.cs
More file actions
45 lines (40 loc) · 2.36 KB
/
Copy pathSqlServerDbContextFactory.cs
File metadata and controls
45 lines (40 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using Microsoft.EntityFrameworkCore;
namespace Ploch.Data.EFCore.SqlServer;
/// <summary>
/// Represents a factory for creating instances of TDbContext configured to use SQL Server as the database provider.
/// This abstract class provides methods to configure the DbContext using a connection string and optional additional
/// SQL Server-specific configuration.
/// </summary>
/// <inheritdoc />
public abstract class SqlServerDbContextFactory<TDbContext, TFactory> : BaseDbContextFactory<TDbContext, TFactory>
where TDbContext : DbContext where TFactory : BaseDbContextFactory<TDbContext, TFactory>
{
/// <summary>
/// Initializes a new instance of the <see cref="SqlServerDbContextFactory{TDbContext, TFactory}" /> class.
/// </summary>
/// <param name="dbContextCreator">Function to create an instance of DbContext.</param>
protected SqlServerDbContextFactory(Func<DbContextOptions<TDbContext>, TDbContext> dbContextCreator) : base(dbContextCreator)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="SqlServerDbContextFactory{TDbContext, TMigrationAssembly}" />
/// class.
/// </summary>
/// <param name="dbContextCreator">Function to create an instance of DbContext.</param>
/// <param name="connectionStringFunc">Function to return the connection string.</param>
protected SqlServerDbContextFactory(Func<DbContextOptions<TDbContext>, TDbContext> dbContextCreator, Func<string> connectionStringFunc) :
base(dbContextCreator, connectionStringFunc)
{ }
/// <summary>
/// Configures the options for the DbContext using the provided connection string function.
/// </summary>
/// <param name="connectionStringFunc">Function to retrieve the database connection string.</param>
/// <param name="optionsBuilder">The options builder to be configured.</param>
/// <returns>The configured DbContextOptionsBuilder.</returns>
protected override DbContextOptionsBuilder<TDbContext> ConfigureOptions(Func<string> connectionStringFunc,
DbContextOptionsBuilder<TDbContext> optionsBuilder)
{
// Correctly configure SQL Server provider for design-time and runtime
optionsBuilder.UseSqlServer(connectionStringFunc(), ApplyMigrationsAssembly);
return optionsBuilder;
}
}