It is a lot easier for metals to lose electrons when they bond with nonmetals. For example, Mg can have a full valence shell of eight if it loses its 2 electrons in its outermost shell. This is a great contrast to Chlorine, for example, which has seven electrons in its valence. It is so much easier for Cl to gain 1 electron than to lose 7, and thus has a higher electron affinity/ionization energy than Mg.