public async Task <Customer> CreateAsync(string firstName, string lastName) { var sql = "INSERT INTO customer.Customers (firstname, lastname) values (@firstName, @lastName) returning id;"; using (var conn = new Npgsql.NpgsqlConnection(ConnectionString)) { var id = await conn.ExecuteScalarAsync <int>(sql, new { firstName, lastName }); return(await GetAsync(id)); } }
/// <summary> /// Execute scalar /// </summary> /// <param name="conn"></param> /// <param name="cmdText"></param> /// <param name="parameters"></param> /// <param name="commandType"></param> /// <returns></returns> public static Task <object> ExecuteScalarAsync(this NpgsqlConnection conn, string cmdText, NpgsqlParameter[] parameters, CommandType commandType) { conn.CheckNull(nameof(conn)); return(conn.ExecuteScalarAsync(cmdText, parameters, commandType, null)); }
/// <summary> /// Execute scalar /// </summary> /// <param name="conn"></param> /// <param name="cmdText"></param> /// <param name="commandType"></param> /// <param name="transaction"></param> /// <returns></returns> public static Task <object> ExecuteScalarAsync(this NpgsqlConnection conn, string cmdText, CommandType commandType, NpgsqlTransaction transaction) { conn.CheckNull(nameof(conn)); return(conn.ExecuteScalarAsync(cmdText, null, commandType, transaction)); }