-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathHrEfDbContext.cs
46 lines (39 loc) · 1.81 KB
/
HrEfDbContext.cs
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
46
using My.Hr.Business.Data.EfModel;
namespace My.Hr.Business.Data;
/// <summary>
/// Represents the Entity Framework <see cref="DbContext"/>.
/// </summary>
public class HrEfDbContext : DbContext, IEfDbContext
{
/// <summary>
/// Initializes a new instance of the <see cref="HrEfDbContext"/> class.
/// </summary>
/// <param name="options">The <see cref="DbContextOptions{HrEfDbContext}"/>.</param>
/// <param name="db">The base <see cref="HrDb"/>.</param>
public HrEfDbContext(DbContextOptions<HrEfDbContext> options, IDatabase db) : base(options) => BaseDatabase = db ?? throw new ArgumentNullException(nameof(db));
/// <summary>
/// Gets the base <see cref="IDatabase"/>.
/// </summary>
public IDatabase BaseDatabase { get; }
/// <summary>
/// Overrides the <see cref="DbContext.OnConfiguring(DbContextOptionsBuilder)"/> to leverage the <see cref="My.Hr.Business.Data.Database"/> connection management.
/// </summary>
/// <param name="optionsBuilder">The <see cref="DbContextOptionsBuilder"/>.</param>
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
// Uses the DB connection management from the database class to ensure the likes of DB connection pooling.
if (!optionsBuilder.IsConfigured)
optionsBuilder.UseSqlServer(BaseDatabase.GetConnection());
}
/// <summary>
/// Overrides the <see cref="DbContext.OnModelCreating(ModelBuilder)"/>.
/// </summary>
/// <param name="modelBuilder">The <see cref="ModelBuilder"/>.</param>
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// Add the generated models to the model builder.
modelBuilder.AddGeneratedModels();
}
}