From ef5c04ad3874e250e432cbed33043fe0cb29b895 Mon Sep 17 00:00:00 2001 From: vivet Date: Sat, 5 Oct 2024 17:42:35 +0200 Subject: [PATCH] Fixed issue with Logger in Dbcontext --- Nano.Data/BaseDbContext.cs | 25 ++++++++++--------------- NanoCore.nuspec | 2 +- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/Nano.Data/BaseDbContext.cs b/Nano.Data/BaseDbContext.cs index 807e637..642cb1c 100644 --- a/Nano.Data/BaseDbContext.cs +++ b/Nano.Data/BaseDbContext.cs @@ -45,11 +45,6 @@ public abstract class BaseDbContext : IdentityDbContext public DataOptions Options { get; } - /// - /// Logger. - /// - public ILogger Logger { get; } - /// /// Auto Save. /// @@ -72,11 +67,9 @@ protected BaseDbContext(DbContextOptions contextOptions, DataOptions dataOptions { this.Options = dataOptions ?? throw new ArgumentNullException(nameof(dataOptions)); - this.Logger = this.GetService(); - this.SavingChanges += (_, _) => this.SetPendingEntityEvents(); this.SavingChanges += (_, _) => this.SaveSoftDeletion(); - this.SavedChanges += async (_, _) => await this.ExecuteEntityEvents(); + this.SavedChanges += async (_, _) => await this.PublishEntityEvents(); // ReSharper disable VirtualMemberCallInConstructor this.ChangeTracker.LazyLoadingEnabled = this.Options.UseLazyLoading; @@ -288,7 +281,9 @@ internal virtual Task EnsureMigratedAsync(CancellationToken cancellationToken = if (this.Options.ConnectionString == null) return Task.CompletedTask; - this.Logger + var logger = this.GetService(); + + logger .LogInformation("Applying Migrations at start-up."); return this.Database @@ -408,15 +403,15 @@ private void SaveAudit(object entity, object tracked = null, EntityEntry owner = try { - var entry = owner == null - ? this.Entry(entity) - : propertName == null - ? this.Entry(entity) + var entry = owner == null + ? this.Entry(entity) + : propertName == null + ? this.Entry(entity) : owner.Reference(propertName).TargetEntry; if (entry == null) { - throw new NullReferenceException(nameof(entry)); + return; } var properties = entity @@ -648,7 +643,7 @@ private object GetNestedPropertyValue(PropertyInfo property, string propertyName return propertyNested .GetValue(parent); } - private async Task ExecuteEntityEvents() + private async Task PublishEntityEvents() { try { diff --git a/NanoCore.nuspec b/NanoCore.nuspec index 683572f..79a2872 100644 --- a/NanoCore.nuspec +++ b/NanoCore.nuspec @@ -2,7 +2,7 @@ NanoCore - 8.1.27 + 8.1.28 Nano Core Rapidly built and deploy rich .NET 8.0 applications. Configuration, Security, Logging, Data, Hosting, Docs, Localization, Versioning, Api, Error-Handling, and much more. The project is inspired by years of tedious repetitions, continuously re-writing similar code-snippets and libraries, to handle common functionality, not related to the business domain, such as logging, data persistence, message queuing, documentation, validation and similar.