using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Text; using System.Linq; using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.EntityFrameworkCore.ChangeTracking; using Newtonsoft.Json.Linq; using Newtonsoft.Json; using System.IO; using System.Security.Claims; using Microsoft.AspNetCore.Components; using Microsoft.EntityFrameworkCore.Metadata.Internal; using System.Data; using Microsoft.Extensions.Configuration; namespace YLShipBuildLandMap.Entity { public partial class YLShipBuildLandMapContext : DbContext { static DbContextOptions dbContextOption = new DbContextOptions(); static DbContextOptionsBuilder dbContextOptionBuilder = new DbContextOptionsBuilder(dbContextOption); public static YLShipBuildLandMapContext New() { var configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json") .Build(); var connection = configuration.GetSection("ConnectionStrings").GetSection("DataContext").Get(); return new YLShipBuildLandMapContext(dbContextOptionBuilder.UseSqlServer(connection, builder => { }).Options); } public static IServiceProvider Services { get; private set; } public static void SetServices(IServiceProvider services) { Services = services; } public override int SaveChanges(bool acceptAllChangesOnSuccess) { LoggingWrite(); return base.SaveChanges(acceptAllChangesOnSuccess); } public override int SaveChanges() { return this.SaveChanges(true); } public override Task SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default) { LoggingWrite(); var tn = base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken); return tn; } protected void LoggingWrite() { IHttpContextAccessor httpContextAccessor = Services.GetService(typeof(IHttpContextAccessor)) as IHttpContextAccessor; List logList = new List(); if (httpContextAccessor != null) { var currHttpContext = httpContextAccessor.HttpContext; if (this.ChangeTracker.Entries() == null) { return; } this.ChangeTracker.Entries().ToList().ForEach(it => { if (it.State == EntityState.Added || it.State == EntityState.Modified || it.State == EntityState.Deleted) { if (!(it.Entity is SysLog)) { var log = LoggingChange(it, currHttpContext); if (log != null) { logList.Add(log); } } } }); } this.SysLog.AddRange(logList); } protected SysLog LoggingChange(EntityEntry entity, HttpContext httpContext) { SysLog sysLog = new SysLog(); sysLog.LogId = Guid.NewGuid(); sysLog.LogTime = DateTime.Now; if (httpContext == null || httpContext.User == null) { string ip = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList[0].ToString(); sysLog.LogType = 1; sysLog.ClientIp = ip; sysLog.LogDescribe = entity.State.ToString(); sysLog.LogTitle = ""; sysLog.LogUser = null; var entityType = entity.Entity.GetType(); var list_methods = entityType.GetProperties().Where(w => !w.GetMethod.IsVirtual).ToList(); Dictionary colums = new Dictionary(); var mapping = this.Model.FindEntityType(entityType); list_methods.ForEach(it => { colums.Add(it.Name, it.GetValue(entity.Entity)); }); sysLog.LogText = JsonConvert.SerializeObject(new { TableName = mapping.GetTableName(), Data = colums }); return sysLog; } try { string ip = httpContext.Connection.RemoteIpAddress.ToString(); var roleClaim = httpContext.User.FindFirst(ClaimTypes.Name); if (roleClaim != null) { sysLog.LogType = 1; sysLog.ClientIp = ip; sysLog.LogDescribe = entity.State.ToString(); sysLog.LogTitle = httpContext.Request.Path.HasValue ? httpContext.Request.Path.Value : ""; sysLog.LogUser = roleClaim.Value; var entityType = entity.Entity.GetType(); var list_methods = entityType.GetProperties().Where(w => !w.GetMethod.IsVirtual).ToList(); Dictionary colums = new Dictionary(); var mapping = this.Model.FindEntityType(entityType); list_methods.ForEach(it => { colums.Add(it.Name, it.GetValue(entity.Entity)); }); sysLog.LogText = JsonConvert.SerializeObject(new { TableName = mapping.GetTableName(), Data = colums }); } } catch (Exception ex) { sysLog.LogText = ex.Message; } return sysLog; } public override Task SaveChangesAsync(CancellationToken cancellationToken = default) { return this.SaveChangesAsync(true, cancellationToken); } [DbFunction(Name = "fn_GetPy", Schema = "dbo")] public static string GetPy(string str) { throw new NotImplementedException(); } [DbFunction(Name = "fn_GetPyFirst", Schema = "dbo")] public static string GetPyFirst(string str) { throw new NotImplementedException(); } [DbFunction(Name = "fn_ReplaceTime", Schema = "dbo")] public static DateTime ReplaceTime(DateTime? dateTimeBase, TimeSpan? timeSpan) { var dateBase = dateTimeBase.Value.Date; if (timeSpan.HasValue) { return dateBase.Add(timeSpan.Value); } else { return dateBase; } } } }