예제 #1
0
        public async Task <TEntity> InsertThenGet(TEntity entity, int?timeoutSeconds = null)
        {
            using (var connection = GetConnection())
            {
                connection.Open();
                var tableName = DbConverter <TEntity> .ToTableName();

                var columnNames = DbConverter <TEntity> .ToColumnNames();

                var parameterNames = DbConverter <TEntity> .ToParameterNames();

                var sql = QueryGenerator.GenerateInsertQuery(tableName, columnNames, parameterNames);
                using (var transaction = connection.BeginTransaction())
                {
                    var insertResult = await connection.ExecuteAsync(sql, entity, commandTimeout : timeoutSeconds);

                    if (insertResult == 1)
                    {
                        const string getLastIdQuery = "SELECT LAST_INSERT_ID();";
                        entity.Id = (await connection.QueryAsync <int>(getLastIdQuery, null, commandTimeout: timeoutSeconds)).First();
                    }
                    else
                    {
                        throw new Exception($"Inserted result must equal to 1: {insertResult}");
                    }
                    transaction.Commit();
                }
                connection.Close();
                return(entity);
            }
        }
예제 #2
0
        public bool TestDbToStringConverter()
        {
            var table = DbConverter <Team> .ToTableName();

            var columnNames = DbConverter <Team> .ToColumnNames(false);

            _logger.Information($"TestDbToStringConverter(): {table} - {columnNames}");
            return(true);
        }
예제 #3
0
        public async Task <int> UpdateAsync(IEnumerable <TEntity> entities, string condition = "Id = @Id", int?timeoutSeconds = null)
        {
            using (var connection = GetConnection())
            {
                connection.Open();
                var tableName = DbConverter <TEntity> .ToTableName();

                var columnNames = DbConverter <TEntity> .ToColumnNames();

                var parameterNames = DbConverter <TEntity> .ToParameterNames();

                var sql = QueryGenerator.GenerateUpdateQuery(tableName, columnNames, parameterNames, condition);
                int updateResult;
                using (var transaction = connection.BeginTransaction())
                {
                    updateResult = await connection.ExecuteAsync(sql, entities, commandTimeout : timeoutSeconds);

                    transaction.Commit();
                }
                connection.Close();
                return(updateResult);
            }
        }