using DataCapture_MA.Entity; using DataCapture_MA.log4Net; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace DataCapture_MA.DataHnadle { /// /// 自动删除数据 /// public class AutoDeleteHistoryDate { public static void Handler() { while (true) { try { using(DbModel dbModel = new DbModel()) { var beforeMonth = DateTimeOffset.Now.AddDays(-30).ToUnixTimeSeconds(); var oldDate = dbModel.RobotInfo.Where(x => x.CreateStamp < beforeMonth).ToList(); if (oldDate.Count > 0) { dbModel.RemoveRange(oldDate); dbModel.SaveChanges(); } } } catch (Exception e) { Log4NetHelper.WriteInfoLog(LogType.PushMes, "删除数据异常:" + JsonConvert.SerializeObject(e)); } Thread.Sleep(1000*60*60); } } } }