diff --git a/PARR.DAL/Context/DataContext.cs b/PARR.DAL/Context/DataContext.cs index ba42d13f..8960bc83 100644 --- a/PARR.DAL/Context/DataContext.cs +++ b/PARR.DAL/Context/DataContext.cs @@ -1,4 +1,5 @@ using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; using PARR.Constants; using PARR.DAL.Contracts; using PARR.DAL.Models; @@ -387,7 +388,59 @@ namespace PARR.DAL.Context } + //protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + //{ + // base.OnConfiguring(optionsBuilder); + // optionsBuilder.UseLoggerFactory(LoggerFactory.Create(builder => + // { + // builder.AddProvider(new MyLoggerProvider()); + // })); + //} + } + + #region MyLoggerProvider + + //https://metanit.com/sharp/efcore/2.12.php + + //internal class MyLoggerProvider : ILoggerProvider + //{ + // public ILogger CreateLogger(string categoryName) + // { + // return new MyLogger(); + // } + + // public void Dispose() + // { + + // } + + + // private class MyLogger : ILogger, IDisposable + // { + // public IDisposable? BeginScope(TState state) where TState : notnull + // { + // return this; + // } + + // public void Dispose() + // { + + // } + + // public bool IsEnabled(LogLevel logLevel) + // { + // return true; + // } + + // public void Log(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func formatter) + // { + // Console.WriteLine("Привет, как дела? {0}", formatter(state, exception)); + // } + // } + //} + + #endregion } diff --git a/PARR.DAL/Services/Abstracts/BaseService.cs b/PARR.DAL/Services/Abstracts/BaseService.cs index fa4e82a3..42f0cf7d 100644 --- a/PARR.DAL/Services/Abstracts/BaseService.cs +++ b/PARR.DAL/Services/Abstracts/BaseService.cs @@ -2,6 +2,7 @@ using Microsoft.Extensions.Logging; using PARR.DAL.Context; using PARR.DAL.DomainModels; +using PARR.DAL.Models; using PARR.DAL.Models.Base; using PARR.DAL.Services.Interfaces.Base; @@ -37,6 +38,40 @@ namespace PARR.DAL.Services.Abstracts public async Task CommitAsync() { + #region test + ////https://stackoverflow.com/questions/53064528/entityframework-core-get-changes-occured-in-entity-and-related-data + //var modifiedEntrities = EntitiContext.ChangeTracker.Entries() + // .Where(t => t.State == EntityState.Modified || t.State == EntityState.Deleted); + + //foreach (var change in modifiedEntrities) + //{ + // ////var type = change.Entity.GetType(); + // ////var userType = typeof(User); + // if (change.Entity.GetType() == typeof(User)) + // { + // // это пользователь + // var user = change.Entity as User; + // if (user!.Name == "string") + // { + // //user.LastLogon = DateTimeOffset.UtcNow; + // user.Description = "Новый description " + DateTime.Now.Microsecond; + // } + + // } + + // // интерфейс IBase? + // //if (change.Entity.GetType().IsAssignableFrom(typeof(IBase))) + + // if (change.Entity.GetType().GetInterface(nameof(IBase)) != null) + // { + // (change.Entity as IBase)!.DateModified = DateTimeOffset.UtcNow; + // } + + //} + + + #endregion + try { await EntitiContext.SaveChangesAsync();