欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

EF SaveChanges重写加入日志

程序员文章站 2022-04-15 21:27:54
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();
        }