public static async Task <int> BatchUpdateAsync <T>(this IQueryable <T> query, T updateValues, List <string> updateColumns = null) where T : class, new()
        {
            DbContext           context    = BatchUtil.GetDbContext(query);
            List <SqlParameter> parameters = new List <SqlParameter>();
            string sql = BatchUtil.GetSqlUpdate(query, context, updateValues, updateColumns, parameters);

            return(await context.Database.ExecuteSqlCommandAsync(sql, parameters.ToArray()));
        }
Esempio n. 2
0
        // Async methods

        public static async Task <int> BatchDeleteAsync <T>(this IQueryable <T> query, CancellationToken cancellationToken = default(CancellationToken)) where T : class
        {
            DbContext context      = BatchUtil.GetDbContext(query);
            DbServer  databaseType = context.Database.ProviderName.EndsWith(DbServer.Sqlite.ToString()) ? DbServer.Sqlite : DbServer.SqlServer;

            (string sql, var sqlParameters) = BatchUtil.GetSqlDelete(query, context);
            return(await context.Database.ExecuteSqlCommandAsync(sql, sqlParameters, cancellationToken).ConfigureAwait(false));
        }
        private static (DbContext, string, List <object>) GetBatchUpdateArguments <T>(IQueryable <T> query, object updateValues = null, List <string> updateColumns = null, Expression <Func <T, T> > updateExpression = null, Type type = null) where T : class
        {
            type ??= typeof(T);
            var context = BatchUtil.GetDbContext(query);

            var(sql, sqlParameters) = updateExpression == null?BatchUtil.GetSqlUpdate(query, context, type, updateValues, updateColumns)
                                          : BatchUtil.GetSqlUpdate(query, context, type, updateExpression);

            return(context, sql, sqlParameters);
        }
Esempio n. 4
0
        /// <summary>
        /// get Update Sql
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="query"></param>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static (string, List <object>) GetSqlUpdate <T>(IQueryable <T> query, Expression <Func <T, T> > expression) where T : class
        {
            DbContext context = BatchUtil.GetDbContext(query);

            (string sql, string tableAlias, string tableAliasSufixAs, IEnumerable <object> innerParameters) = GetBatchSql(query, context, isUpdate: true);
            var sqlColumns          = new StringBuilder();
            var sqlParameters       = new List <object>(innerParameters);
            var columnNameValueDict = TableInfo.CreateInstance(GetDbContext(query), new List <T>(), OperationType.Read, new BulkConfig()).PropertyColumnNamesDict;

            CreateUpdateBody(columnNameValueDict, tableAlias, expression.Body, ref sqlColumns, ref sqlParameters);

            sqlParameters = ReloadSqlParameters(context, sqlParameters); // Sqlite requires SqliteParameters
            sqlColumns    = (GetDatabaseType(context) == DbServer.SqlServer) ? sqlColumns : sqlColumns.Replace($"[{tableAlias}].", "");

            var resultQuery = $"UPDATE {tableAlias}{tableAliasSufixAs} SET {sqlColumns} {sql}";

            return(resultQuery, sqlParameters);
        }
Esempio n. 5
0
 public static async Task BatchUpdateAsync <T>(this IQueryable <T> query, DbContext context, T updateValues, List <string> updateColumns = null) where T : class, new()
 {
     string sql = BatchUtil.GetSqlUpdate(query, context, updateValues, updateColumns);
     await context.Database.ExecuteSqlCommandAsync(sql);
 }
Esempio n. 6
0
        // Async methods

        public static async Task BatchDeleteAsync <T>(this IQueryable <T> query, DbContext context) where T : class, new()
        {
            string sql = BatchUtil.GetSqlDelete(query, context);
            await context.Database.ExecuteSqlCommandAsync(sql);
        }
Esempio n. 7
0
        public static void BatchDelete <T>(this IQueryable <T> query, DbContext context) where T : class, new()
        {
            string sql = BatchUtil.GetSqlDelete(query, context);

            context.Database.ExecuteSqlCommand(sql);
        }
        public static Task BatchInsertAsync <T>(this IQueryable query, IList <T> entities, BulkConfig bulkConfig = null, CancellationToken cancellationToken = default) where T : class
        {
            var context = BatchUtil.GetDbContext(query);

            return(DbContextBulkTransaction.ExecuteAsync(context, entities, OperationType.Insert, bulkConfig, null, cancellationToken));
        }
        public static void BatchInsert <T>(this IQueryable query, IList <T> entities, BulkConfig bulkConfig = null) where T : class
        {
            var context = BatchUtil.GetDbContext(query);

            DbContextBulkTransaction.Execute(context, entities, OperationType.Insert, bulkConfig, null);
        }