public static async Task SaveAllMetadatasAsync(this MediaMetaDatabaseContext context, ICollection <MediaMetadata> metas) { // Time and log serialization. using (new DisposableLogger(DatabaseLog.SerializationBegin, (sw) => DatabaseLog.SerializationEnd(sw, metas.Count))) { var fullList = await context.MediaMetaJsons.Include(m => m.Labels).ToListAsync(); foreach (var meta in metas) { // If any MediaMetaJson has a set of labels that are equal to the ones of meta, then update it. var toUpdate = fullList.FirstOrDefault(mmj => { return(mmj.Labels.ListEquals(meta.Labels)); }); // If there is an existing item to update. if (toUpdate != null) { var metaJson = await MediaMetaJson.FromMediaMetaAsync(meta); toUpdate.Json = metaJson.Json; } else // If a new item needs to be added. { meta.DateAdded = DateTime.Now; var metaJson = await MediaMetaJson.FromMediaMetaAsync(meta); context.MediaMetaJsons.Add(metaJson); } } } try { // Time and log saving of MediaMetaJsons. using (new DisposableLogger(DatabaseLog.SaveBegin, (sw) => DatabaseLog.SaveEnd(sw, metas.Count))) { await context.SaveChangesAsync(); } } catch (Exception e) { // Log Exception. LifecycleLog.Exception(e); } }