public override int SaveChanges() { var entries = ChangeTracker.Entries().Where(x => x.State != EntityState.Unchanged).GroupBy(x => new { x.State, x.E ...
public override int SaveChanges() { var entries = ChangeTracker.Entries().Where(x => x.State != EntityState.Unchanged).GroupBy(x => new { x.State, x.Entity.GetType().Name }).Select(x => new { x.Key.State, x.Key.Name, List = x.ToList() }); JsonSerializerSettings settings = new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore// 發現迴圈引用時跳過序列化 }; foreach (var item in entries) { Logger.WriteOperationlog($"\r\n Operation {item.State} Date:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}\r\n Entity:{item.Name}\r\n Data:{JsonConvert.SerializeObject(item.List.Select(x => x.Entity).ToList(), settings)}", "Default"); } return base.SaveChanges(); }