Example #1
0
        /// <example>
        /// context.Items.Where(a => a.ItemId >  500).BatchDelete();
        /// </example>
        public static int BatchDelete(this IQueryable query)
        {
            var context = query.GetDbContext();

            (string sql, var parameters) = BatchUtil.GetSqlDelete(query, context);

            return(context.Database.ExecuteSqlRaw(sql, parameters));
        }
Example #2
0
        public static async Task <int> BatchUpdateAsync(this IQueryable query,
                                                        Type type,
                                                        Expression <Func <object, object> > updateExpression,
                                                        CancellationToken cancellationToken = default)
        {
            var context = query.GetDbContext();

            var(sql, parameters) = BatchUtil.GetSqlUpdate(query, context, type, updateExpression);
            return(await context.Database.ExecuteSqlRawAsync(sql, parameters, cancellationToken).ConfigureAwait(false));
        }
Example #3
0
        /// <example>
        /// context.Items.Where(x => x.ItemId <= 500).BatchUpdateAsync(x => new Item { Quantity = x.Quantity + 100 });
        /// </example>
        public static async Task <int> BatchUpdateAsync <T>(this IQueryable <T> query,
                                                            Expression <Func <T, T> > updateExpression,
                                                            CancellationToken cancellationToken = default)
            where T : BaseEntity, new()
        {
            var context = query.GetDbContext();

            var(sql, parameters) = BatchUtil.GetSqlUpdate(query, context, updateExpression);
            return(await context.Database.ExecuteSqlRawAsync(sql, parameters, cancellationToken).ConfigureAwait(false));
        }
Example #4
0
        /// <example>
        /// context.Items.Where(x => x.ItemId <= 500).BatchUpdateAsync(new Item { Quantity = x.Quantity + 100 }, nameof(Item.Quantity));
        /// </example>
        public static async Task <int> BatchUpdateAsync(this IQueryable query,
                                                        object updateValues,
                                                        IEnumerable <string> updateColumns  = null,
                                                        CancellationToken cancellationToken = default)
        {
            var context = query.GetDbContext();

            var(sql, parameters) = BatchUtil.GetSqlUpdate(query, context, updateValues, updateColumns?.ToList());
            return(await context.Database.ExecuteSqlRawAsync(sql, parameters, cancellationToken).ConfigureAwait(false));
        }