This commit is contained in:
2026-04-24 00:33:07 +03:30
parent 2f70fb0fef
commit 37e537f385
2 changed files with 42 additions and 25 deletions
+27 -11
View File
@@ -1,12 +1,14 @@
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using xDataService.Configuration; using xDataService.Configuration;
namespace xDataService.Db { namespace xDataService.Db
{
/// <summary> /// <summary>
/// Provide an abstraction layout arround DBContext ... /// Provide an abstraction layout arround DBContext ...
/// Only Used on EFCore ... /// Only Used on EFCore ...
/// </summary> /// </summary>
public abstract class XDbContext : DbContext { public abstract class XDbContext : DbContext
{
private readonly XDataServiceConfiguration config; private readonly XDataServiceConfiguration config;
// //
@@ -20,22 +22,30 @@ namespace xDataService.Db {
public XDbContext( public XDbContext(
DbContextOptions options, DbContextOptions options,
XDataServiceConfiguration config XDataServiceConfiguration config
) : base (options) { ) : base(options)
{
// //
try { try
{
Database.EnsureCreated(); Database.EnsureCreated();
Database.Migrate(); Database.Migrate();
} catch { } }
catch { }
this.config = config; this.config = config;
} }
// //
// Prepare Fluent API ... // Prepare Fluent API ...
protected override void OnModelCreating (ModelBuilder modelBuilder) { protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// //
OnXModelCreating(modelBuilder); OnXModelCreating(modelBuilder);
//
// Automatically apply all IEntityTypeConfiguration<T> in this assembly
modelBuilder.ApplyConfigurationsFromAssembly(typeof(XDbContext).Assembly);
// //
// Since this class inherit from Identity Db Context, // Since this class inherit from Identity Db Context,
// we have to pass this to Building Identity Models ... // we have to pass this to Building Identity Models ...
@@ -47,21 +57,27 @@ namespace xDataService.Db {
// //
// Prepare Configuring ... // Prepare Configuring ...
protected override void OnConfiguring (DbContextOptionsBuilder optionsBuilder) { protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
// //
if (config.EnableDetailedErrors) { if (config.EnableDetailedErrors)
{
optionsBuilder.EnableDetailedErrors(); optionsBuilder.EnableDetailedErrors();
} }
// //
if (config.EnableSensitiveDataLogging) { if (config.EnableSensitiveDataLogging)
{
optionsBuilder.EnableSensitiveDataLogging(); optionsBuilder.EnableSensitiveDataLogging();
} }
// //
if (config.EnableTracking) { if (config.EnableTracking)
{
optionsBuilder.UseQueryTrackingBehavior(QueryTrackingBehavior.TrackAll); optionsBuilder.UseQueryTrackingBehavior(QueryTrackingBehavior.TrackAll);
} else { }
else
{
optionsBuilder.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking); optionsBuilder.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
} }
+2 -1
View File
@@ -27,7 +27,8 @@
<!-- Local Dependencies --> <!-- Local Dependencies -->
<ItemGroup> <ItemGroup>
<ProjectReference Include="../xModels/xModels.csproj" /> <!-- <ProjectReference Include="../xModels/xModels.csproj" /> -->
<PackageReference Include="xDashboard.xModels" Version="1.0.0" />
</ItemGroup> </ItemGroup>
<!-- Dependencies --> <!-- Dependencies -->