static void Main(string[] args) { var configuration = GetConfiguration(); try { var cfg = configuration.GetAPIConfig(); var contextOptions = new DbContextOptionsBuilder <DemoContext>() .UseSqlServer(cfg.DatabaseConnectionString) .Options; using var ctx = new DemoContext(contextOptions); var oldLogs = ctx .AuditLogs .Where(al => al.TimeStamp < DateTime.UtcNow.AddDays(cfg.AuditLogRetentionDays)); ctx.RemoveRange(oldLogs); ctx.SaveChanges(); Console.WriteLine("Audit Log cleaned up."); } catch (Exception exception) { Console.WriteLine($"ERROR while removing old AuditLogs."); Console.WriteLine(exception.Message); Console.WriteLine(exception.StackTrace); } }
public async Task <bool> PostAsync([FromBody] Dictionary <string, string> data) { var keys = data.Keys; var entities = await context.Configs.Where(x => keys.Contains(x.Key)).ToListAsync(); context.RemoveRange(entities); foreach (var kv in data) { var entity = new ConfigEntity { Key = kv.Key, Value = kv.Value }; context.Add(entity); } var result = await context.SaveChangesAsync(); return(result > 0); }