94 lines
2.5 KiB
C#
94 lines
2.5 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using xDataService.Configuration;
|
|
|
|
namespace xDataService.Db
|
|
{
|
|
/// <summary>
|
|
/// Provide an abstraction layout arround DBContext ...
|
|
/// Only Used on EFCore ...
|
|
/// </summary>
|
|
public abstract class XDbContext : DbContext
|
|
{
|
|
private readonly XDataServiceConfiguration config;
|
|
|
|
//
|
|
#region Entities ...
|
|
//
|
|
// Register Default and Base Entities ...
|
|
#endregion
|
|
|
|
//
|
|
// Constructor ...
|
|
public XDbContext(
|
|
DbContextOptions options,
|
|
XDataServiceConfiguration config
|
|
) : base(options)
|
|
{
|
|
//
|
|
try
|
|
{
|
|
Database.EnsureCreated();
|
|
Database.Migrate();
|
|
}
|
|
catch { }
|
|
|
|
this.config = config;
|
|
}
|
|
|
|
//
|
|
// Prepare Fluent API ...
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
//
|
|
OnXModelCreating(modelBuilder);
|
|
|
|
//
|
|
// Automatically apply all IEntityTypeConfiguration<T> in this assembly
|
|
modelBuilder.ApplyConfigurationsFromAssembly(typeof(XDbContext).Assembly);
|
|
|
|
//
|
|
// Since this class inherit from Identity Db Context,
|
|
// we have to pass this to Building Identity Models ...
|
|
base.OnModelCreating(modelBuilder);
|
|
}
|
|
|
|
//
|
|
public abstract void OnXModelCreating(ModelBuilder modelBuilder);
|
|
|
|
//
|
|
// Prepare Configuring ...
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|
{
|
|
//
|
|
if (config.EnableDetailedErrors)
|
|
{
|
|
optionsBuilder.EnableDetailedErrors();
|
|
}
|
|
|
|
//
|
|
if (config.EnableSensitiveDataLogging)
|
|
{
|
|
optionsBuilder.EnableSensitiveDataLogging();
|
|
}
|
|
|
|
//
|
|
if (config.EnableTracking)
|
|
{
|
|
optionsBuilder.UseQueryTrackingBehavior(QueryTrackingBehavior.TrackAll);
|
|
}
|
|
else
|
|
{
|
|
optionsBuilder.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
|
|
}
|
|
|
|
//
|
|
OnXConfiguring(optionsBuilder);
|
|
|
|
//
|
|
base.OnConfiguring(optionsBuilder);
|
|
}
|
|
|
|
//
|
|
public abstract void OnXConfiguring(DbContextOptionsBuilder optionsBuilder);
|
|
}
|
|
} |