When an element combines with another element by losing and gaining electrons to form a compound, its valency is called electrovalency.
Electrovalency in Magnesium :
Magnesium atom has atomic number . Thus, the number of electrons in a Magnesium atom are .
The electronic configuration of Magnesium is .
In order to acquire eight electrons in the outermost shell i.e. to attain nearest noble gas configuration, Magnesium atom loses electrons from the outermost shell.
This leads to formation of Magnesium ion having two units of positive charges .