public static async Task AddItemSellable(List <Item> items, List <ItemListingAggregated> listings) { using (var db = new DataContextFactory().CreateDbContext()) { var existingItems = await db.ItemSellable.Where(x => items.Select(y => y.ID).Contains(x.ItemID)).ToListAsync(); foreach (var item in items) { var isSellable = listings.Any(x => x.ItemID == item.ID); if (existingItems.Any(x => x.ItemID == item.ID)) { var existingItem = existingItems.FirstOrDefault(x => x.ItemID == item.ID); db.Attach(existingItem); existingItem.Sellable = isSellable; } else { db.ItemSellable.Add(new ItemSellable { ItemID = item.ID, Sellable = isSellable }); } } await db.SaveChangesAsync(); } }