private static BulkInsertResult <T> BulkInsert <T>(IEnumerable <T> entities, BulkOptions options, TableMapping tableMapping, SqlConnection dbConnection, SqlTransaction transaction, string tableName,
                                                           IEnumerable <string> inputColumns = null, SqlBulkCopyOptions bulkCopyOptions = SqlBulkCopyOptions.Default, bool useInteralId = false)
        {
            var dataReader = new EntityDataReader <T>(tableMapping, entities, useInteralId);

            var sqlBulkCopy = new SqlBulkCopy(dbConnection, bulkCopyOptions, transaction)
            {
                DestinationTableName = tableName,
                BatchSize            = options.BatchSize
            };

            if (options.CommandTimeout.HasValue)
            {
                sqlBulkCopy.BulkCopyTimeout = options.CommandTimeout.Value;
            }
            foreach (var property in dataReader.TableMapping.Properties)
            {
                if (inputColumns == null || (inputColumns != null && inputColumns.Contains(property.Name)))
                {
                    sqlBulkCopy.ColumnMappings.Add(property.Name, property.Name);
                }
            }
            if (useInteralId)
            {
                sqlBulkCopy.ColumnMappings.Add(Constants.InternalId_ColumnName, Constants.InternalId_ColumnName);
            }
            sqlBulkCopy.WriteToServer(dataReader);

            return(new BulkInsertResult <T>
            {
                RowsAffected = sqlBulkCopy.RowsCopied,
                EntityMap = dataReader.EntityMap
            });
        }
        private static BulkQueryResult BulkQuery(this DbContext context, string sqlText, SqlConnection dbConnection, SqlTransaction transaction, BulkOptions options)
        {
            var results = new List <object[]>();
            var columns = new List <string>();
            var command = new SqlCommand(sqlText, dbConnection, transaction);

            if (options.CommandTimeout.HasValue)
            {
                command.CommandTimeout = options.CommandTimeout.Value;
            }
            var reader = command.ExecuteReader();

            //Get column names
            for (int i = 0; i < reader.FieldCount; i++)
            {
                columns.Add(reader.GetName(i));
            }
            try
            {
                //Read data
                while (reader.Read())
                {
                    Object[] values = new Object[reader.FieldCount];
                    reader.GetValues(values);
                    results.Add(values);
                }
            }
            finally
            {
                //close the DataReader
                reader.Close();
            }

            return(new BulkQueryResult
            {
                Columns = columns,
                Results = results,
                RowsAffected = reader.RecordsAffected
            });
        }