public async Task UpdateAsync(CitizenEntity[] entities) { if (entities == null || !entities.Any()) { return; } using (var context = new CitizensContext()) { DateTime oldCdate; DateTime udate = DateTime.UtcNow; foreach (var entity in entities) { var oldEntity = await context.Citizens.FirstOrDefaultAsync(a => a.id == entity.id).ConfigureAwait(false); if (oldEntity == null) { throw new Exception($"Citizen with such ID is not found. ID = {entity.id}"); } oldCdate = oldEntity.cdate; entity.CopyTo(oldEntity); oldEntity.cdate = oldCdate; oldEntity.udate = udate; } await context.SaveChangesAsync().ConfigureAwait(false); } }
public async Task CreateAsync(CitizenEntity[] entities) { if (entities == null || !entities.Any()) { return; } using (var context = new CitizensContext()) { var cdate = DateTime.UtcNow; foreach (var entity in entities) { entity.cdate = entity.udate = cdate; context.Citizens.Add(entity); } await context.SaveChangesAsync().ConfigureAwait(false); } }
public async Task DeleteAsync(int[] ids) { if (ids == null || !ids.Any()) { throw new ArgumentNullException("Citizen IDs are not specified"); } using (var context = new CitizensContext()) { foreach (var id in ids) { var oldEntity = await context.Citizens.FirstOrDefaultAsync(a => a.id == id).ConfigureAwait(false); if (oldEntity == null) { throw new Exception($"Citizen with such ID is not found. ID = {id}"); } context.Citizens.Remove(oldEntity); } await context.SaveChangesAsync().ConfigureAwait(false); } }