Beispiel #1
0
        public async Task <int> BulkMergeAsync(IQueryable <TEntity> queryable, List <TEntity> current)
        {
            var previous = await queryable.ToListAsync();

            var toDelete = previous
                           .Where(p => current.All(c => !PrimaryKeyEquals(c, p)))
                           .ToList();

            var toAdd = current
                        .Where(c => previous.All(p => !PrimaryKeyEquals(c, p)))
                        .ToList();

            var toUpdate = previous
                           .Where(p => current.Any(c => PrimaryKeyEquals(c, p)))
                           .ToList();

            DbContext.RemoveRange(toDelete);

            await DbContext.AddRangeAsync(toAdd);

            foreach (var entity in toUpdate)
            {
                var updatedEntity = current.First(c => PrimaryKeyEquals(c, entity));

                foreach (var property in Properties)
                {
                    property.PropertyInfo.SetValue(entity, property.PropertyInfo.GetValue(updatedEntity));
                }
            }

            return(await DbContext.SaveChangesAsync());
        }
        public async Task <int> BulkMergeAsync(
            IQueryable <TEntity> queryable,
            List <TEntity> current,
            Func <TEntity, object> updateColumnsToExlcude  = null,
            BulkMergeNotMatchedBehavior notMatchedBehavior = BulkMergeNotMatchedBehavior.DoNothing,
            Expression <Func <TEntity> > whenNotMatched    = null)
        {
            var previous = await queryable.ToListAsync();

            var notMatched = previous
                             .Where(p => current.All(c => !PrimaryKeyEquals(c, p)))
                             .ToList();

            var toAdd = current
                        .Where(c => previous.All(p => !PrimaryKeyEquals(c, p)))
                        .ToList();

            var toUpdate = previous
                           .Where(p => current.Any(c => PrimaryKeyEquals(c, p)))
                           .ToList();

            switch (notMatchedBehavior)
            {
            case BulkMergeNotMatchedBehavior.Delete:
                DbContext.RemoveRange(notMatched);
                break;

            case BulkMergeNotMatchedBehavior.Update when whenNotMatched != null:
                foreach (var entity in notMatched)
                {
                    var notMatchedProps  = whenNotMatched.GetSetPropertyNames();
                    var notMatchedEntity = whenNotMatched.Compile().Invoke();

                    foreach (var property in Properties.Where(x => notMatchedProps.Contains(x.Name)))
                    {
                        property.PropertyInfo.SetValue(entity, property.PropertyInfo.GetValue(notMatchedEntity));
                    }
                }
                break;
            }

            await DbContext.AddRangeAsync(toAdd);

            foreach (var entity in toUpdate)
            {
                var updatedEntity = current.First(c => PrimaryKeyEquals(c, entity));

                foreach (var property in Properties)
                {
                    property.PropertyInfo.SetValue(entity, property.PropertyInfo.GetValue(updatedEntity));
                }
            }

            return(await DbContext.SaveChangesAsync());
        }
Beispiel #3
0
        public async Task BulkAddAsync(List <TEntity> entities)
        {
            await DbContext.AddRangeAsync(entities);

            await DbContext.SaveChangesAsync();
        }