Kidney stones and magnesium deficiency share the same list of causes, including a diet high in sugar, alcohol, oxalates, and coffee. Magnesium has an important role in the prevention of kidney stone formation. Magnesium stimulates production of calcitonin, which draws calcium out of the blood and soft tissues back into the bones, preventing some forms of arthritis and kidney stones. Magnesium suppresses parathyroid hormone, preventing it from breaking down bone. Magnesium converts vitamin D into its active form so that it can assist in calcium absorption. Magnesium is required to activate an enzyme that is necessary to form new bone. Magnesium regulates active calcium transport. All these factors help place calcium where it needs to be, and not in kidney stones.
One of magnesium's many jobs is to keep calcium in solution to prevent it from solidifying into crystals; even at times of dehydration, if there is sufficient magnesium, calcium will stay in solution. Magnesium is a pivotal treatment for kidney stones. If you don't have enough magnesium to help dissolve calcium, you will end up with various forms of calcification. This translates into stones, muscle spasms, fibrositis, fibromyalgia, and atherosclerosis (as in calcification of the arteries). Dr. George Bunce has clinically demonstrated the relationship between kidney stones and magnesium deficiency. As early as 1964, Bunce reported the benefits of administering a 420 mg dose of magnesium oxide per day to patients who had a history of frequent stone formation.[14,15] If poorly absorbed magnesium oxide works, other forms of better-absorbed magnesium will work better.
Calcium oxalate stones can effectively be prevented by getting an adequate amount of magnesium, either through foods high in magnesium (buckwheat, green vegetables, beans, nuts), or magnesium supplements. Take a magnesium supplement of at least the US RDA of 300-400 mg/day (more may be desirable in order to maintain an ideal 1:1 balance of magnesium to calcium). To prevent a laxative effect, take a supplement that is readily absorbable, such as magnesium citrate, chelate, malate, or chloride. Magnesium oxide, mentioned above, is cheap and widely available. However, magnesium oxide is only about 5% absorbed and thus acts mostly as a laxative.  Milk of magnesia (magnesium hydroxide) is even more of a laxative, and unsuitable for supplementation. Magnesium citrate is a good choice: easy to find, relatively inexpensive and well absorbed.
The Role of Vitamin C in Preventing and Dissolving Kidney Stones
The calcium phosphate kidney stone can only exist in a urinary tract that is not acidic. Ascorbic acid (vitamin C's most common form) acidifies the urine, thereby dissolving phosphate stones and preventing their formation.
Acidic urine will also dissolve magnesium ammonium phosphate stones, which would otherwise require surgical removal. These are the same struvite stones associated with urinary tract infections. Both the infection and the stone are easily cured with vitamin C in large doses. Both are virtually 100% preventable with daily consumption of much-greater-than-RDA amounts of ascorbic acid. A gorilla gets about 4,000 mg of vitamin C a day in its natural diet. The US RDA for humans is only 90 mg. The gorillas are unlikely to all be wrong.
The common calcium oxalate stone can form in an acidic urine whether one takes vitamin C or not. However, this type of stone can be prevented by adequate quantities of B-complex vitamins and magnesium. Any common B-complex supplement, twice daily, plus about 400 milligrams of magnesium, is usually adequate.
A Dozen Ways to Reduce Your Risk of Kidney Stones
1. Maximize fluid intake. Especially drink fruit and vegetable juices. Orange, grape and carrot juices are high in citrates which inhibit both a buildup of uric acid and also stop calcium salts from forming. 
2. Control urine pH. Slightly acidic urine helps prevent urinary tract infections, dissolves both phosphate and struvite stones, and will not cause oxalate stones. And of course one way to make urine slightly acidic is to take vitamin C.
3. Avoid excessive oxalates by not eating (much) rhubarb, spinach, chocolate, or dark tea or coffee.
4. Lose weight. Being overweight is associated with substantially increased risk of kidney stones.
5. Calcium is probably not the real culprit. Low calcium may itself cause calcium stones .
6. Most kidney stones are compounds of calcium and yet many Americans are calcium deficient. Instead of lowering calcium intake, reduce excess dietary phosphorous by avoiding carbonated soft drinks, especially colas. Cola soft drinks contain excessive quantities of phosphorous as phosphoric acid. This is the same acid that is used by dentists to dissolve tooth enamel before applying bonding resins.
7. Take a magnesium supplement of at least the US RDA of 300-400 mg/day. More may be desirable in order to maintain an ideal 1:1 balance of magnesium to calcium. Many people eating "modern" processed-food diets do not consume optimal quantities of magnesium.
8. Take a good B-complex vitamin supplement twice daily, which contains pyridoxine (vitamin B6). A deficiency of vitamin B6 produces kidney stones in experimental animals. Vitamin B6 deficiency is very common in humans. A vitamin B1 (thiamine) deficiency also is associated with stones. 
9. For uric acid/purine stones (gout), stop eating meat. Nutrition tables and textbooks indicate meats as the major dietary purine source. Natural treatment adds juice fasts and eating sour cherries. Increased vitamin C consumption helps by improving the urinary excretion of uric acid. . For these stones, use buffered ascorbate "C".
10. Persons with cystine stones (only 1% of all kidney stones) should follow a low methionine diet and use buffered vitamin C.
11. Kidney stones are associated with high sugar intake, so eat less (or no) added sugar. 
12. Infections can cause conditions that favor stone formation, such as overly concentrated urine (from fever sweating, vomiting or diarrhea). Practice good preventive health care, and it will pay you back with interest.