VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > 简明python教程 >
  • EF SaveChanges重写加入日志

复制代码
 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();
        }
复制代码

相关教程