For an ideal gas, we can write the internal energy, U, as a function of temperature and volume.
U = U(T,V)
From this expression, we can obtain the following differential:
dU = (dU/dT)vdT + (dU/dV)TdV
Note that Cv = (dU/dT)v by definition.
In addition, (dU/dV)T = 0 because the internal energy of an ideal gas depends only on its temperature.
Therefore, we get:
dU = CvdT
This equation works in all cases (but only for ideal gases. If (dU/dV)T is not zero, this does not hold).
dq = CvdT is only valid for constant pressure processes, however.