public static async Task BuildCache <TEntity, TCache>(TreasureEntities entities, IMapper mapper, CacheItemType type, DateTimeOffset timestamp) where TEntity : class { Debug.WriteLine("Building cache for " + type); var data = entities.Set <TEntity>().AsQueryable(); var cast = await data.ProjectTo <TCache>(mapper.ConfigurationProvider).ToListAsync(); var json = JsonConvert.SerializeObject(cast, JsonSettings); var existing = await entities.CacheSets.SingleOrDefaultAsync(x => x.Type == type); var existed = existing != null; if (!existed) { existing = new CacheSet { Type = type }; entities.CacheSets.Add(existing); } existing.JSON = json; existing.EditedDate = timestamp; await entities.SaveChangesAsync(); Debug.WriteLine("Cache built."); }
private static async Task PostRun(TreasureEntities context, IMapper mapper) { await context.SaveChangesAsync(); var timestamp = DateTimeOffset.UtcNow; await CacheBuilder.BuildCache <Unit, UnitStubModel>(context, mapper, CacheItemType.Unit, timestamp); await CacheBuilder.BuildCache <Stage, StageStubModel>(context, mapper, CacheItemType.Stage, timestamp); await CacheBuilder.BuildCache <Ship, ShipStubModel>(context, mapper, CacheItemType.Ship, timestamp); }
private static async Task PreRun(TreasureEntities context) { context.Teams.Clear(); context.TeamUnits.Clear(); context.ScheduledEvents.Clear(); context.StageAliases.Clear(); context.Stages.Clear(); context.Ships.Clear(); context.UnitAliases.Clear(); context.UnitEvolutions.Clear(); context.Units.Clear(); context.CacheSets.Clear(); await context.SaveChangesAsync(); }
public static async Task SaveChangesSafe(this TreasureEntities entities) { try { await entities.SaveChangesAsync(); } catch (DbEntityValidationException dbEx) { Exception raise = dbEx; foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { var message = $"{validationErrors.Entry.Entity}:{validationError.ErrorMessage}"; // raise a new exception nesting // the current instance as InnerException raise = new InvalidOperationException(message, raise); } } throw raise; } }