I am working on a project on which I need to calculate the geomagnetic field in different coordinates. When I use the conventional form of the dipole field in spherical coordinates: $$\vec{B}_{r,\phi}=\frac{B_0 R^3}{r^3}(-2 \cos \phi\, \hat{e}_r- \sin \phi\, \hat{e}_\phi),$$ with $B_0$ as the field intensity in the equator, $R$ the radius of the Earth, $r$ is an arbitrary distance from the center from the center, $\phi$ is the polar angle. The divergence of this field is zero. $$\nabla \cdot \vec{B}_{r,\phi}=0.$$ When I transform it to cartesian coordinates $\hat{I}$,$\hat{J}$,$\hat{K}$ $$\vec{B}_{X,Y,Z}=\frac{B_0 R^3}{(X^2+Y^2+Z^2)^{5/2}}[-3 X Z\, \hat{I} -3 YZ\, \hat{J}+(X^2+Y^2-2Z^2)\hat{K}],$$ the divergence of this field is also zero $$\nabla \cdot \vec{B}_{X,Y,Z}=0$$ The transformation on which I am having trouble is happening when I convert the $\vec{B}_{X,Y,Z}$ to the cyllindrical orbital coordinates through and ordinary rotation matrix 3-1-3, wehre the first rotation is about the $\hat{K}$-axis, the longitude of ascending node $\Omega$, the second around the new $\hat{X}^\prime$-axis, the inclination $i$, and the final rotation around the $\hat{K}^\prime$-axis, is the sum of the periapsos argument $\omega$ and the true anomaly $f$, so the total angle of this final rotation is $\omega+f$, and the variables $X$, $Y$ and $Z$ are given by: $$X=r (\sin (f) (\cos (i) \cos (\omega ) \sin (\Omega )+\sin (\omega ) \cos (\Omega ))+\cos (f) (\cos (i) \sin (\omega ) \sin (\Omega )-\cos (\omega ) \cos (\Omega )));$$ $$Y=-r(\cos (i) \cos (\Omega ) \sin (f+\omega )+\sin (\Omega ) \cos (f+\omega ));$$ $$Z=-r \sin (i) \sin (f+\omega ).$$ When I do these transformations, I obtain a magnetic field in this form: $$\vec{B}_{r,f}=\frac{B_0 R^3}{r^3}[-2 \sin i \sin (\omega+f)\, \hat{r} + \sin i \cos(\omega+f)\, \hat{f} + \cos i\, \hat{k}],$$ since is a cyllindrical frame, I choosed to name the the basis as $\hat{r}$, $\hat{f}$ and $\hat{k}$, wehre the variable $r$ has the same interpretation of distance and the variable $f$, the true anomaly is the angle analogous to an angle of cyllindrical coordinates.
However when I take the divergence of $\vec{B}_{r,f}$, it is not equals zero, actually: $$\nabla \cdot \vec{B}_{r,f}=\frac{3 B_0 R^3 \sin i \sin (\omega+f)}{r^4}.$$ I know that Maxwell's equations states that the divergence of any magnetic field is zero so I am just wondering why this is happening, I am pretty sure that the transformations are correct, but I can't actually understand why the divergence of this field is non-zero.
Obs: I also made the transformation using the electromagnetic field tensor formalism for the rotation and it gives the same result as the commom vector transformation by a matrix of rotation.