public async Task SaveAsync<T>(IEnumerable<T> models)
            where T : BaseModel
        {
            var existingIds = _dbContext.Set<T>().Select(m => m.Id);

            _dbContext.UpdateRange(models.Where(m => existingIds.Contains(m.Id)));
            await _dbContext.AddRangeAsync(models.Where(m => !existingIds.Contains(m.Id)));
            await _dbContext.SaveChangesAsync();
        }
예제 #2
0
        private async Task SyncModels <T>(IEnumerable <T> models, IQueryable <T> dbSet)
            where T : BaseModel
        {
            var existingIds = dbSet.Select(m => m.Id);
            var modelIds    = models.Select(m => m.Id);

            _dbContext.RemoveRange(dbSet.Where(m => !modelIds.Contains(m.Id)));
            _dbContext.UpdateRange(models.Where(m => existingIds.Contains(m.Id)));
            await _dbContext.AddRangeAsync(models.Where(m => !existingIds.Contains(m.Id)));

            await _dbContext.SaveChangesAsync();
        }