I often get vertigo when sleeping, why does this happen and how can I prevent it?

BPPV. If you are getting vertigo while sleeping... It is likely benign positional vertigo and you are having symptoms because you are laying down and turning your head during sleep. If it is bppv then your symptoms are likely lasting several seconds and then subside. Seeing your local ENT would be a good start. They can actually treat you for this in their clinic if it is bppv.
Maybe loose crystals. If it happens when you are laying down and turn from one side to another it may be something called benign positional vertigo. In everyone's ears there a small organs that have calcium deposits on top of them. These calcium deposits (crystals, stones, etc...) can become loose and roll around. When they float around they can cause vertigo. See an ENT doctor who can make this diagnosis and cure it.
Abnormal symptom. This symptom of vertigo during sleep can have several causes and requires an investigation by your physician to determine the cause and proper treatment. E.G. This can be an inner ear problem, or a vascular problem, or a problem related to your brain or related to blood pressure issues or medication along with several other conditions.