protected virtual IQueryable <TEntity> FetchEntities(TKey id = default(TKey)) { var queryable = DbContext.Set <TEntity>().AsQueryable(); if (!IsUnspecified(id)) { queryable = queryable.FindId(id); } queryable = Filter(queryable); return(queryable); }
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."); }