Are you running the most recent version of iOS? You can check in Settings > General > About.
Previous version had a bug that would occasionally delete all contacts if 1) you had a very large contact database and 2) were not syncing the contacts with any online service.
I had not heard of this happening recently, so I was hoping that it was squashed with one of the 5.x updates.
At any rate, the best way to avoid this is to sync with an online service. iCloud is the easiest and supports the most features. GMail is also a good choice, though you need to set your
GMail account up as an Exchange account, instead of the default IMAP account. Yahoo will also work, though reading posts here gives me the impression there servers are not as reliable as iCloud and GMail.
If you do sync with one of the online services, be sure you turn the syncing you set up in iTunes off (in the Info tab); or you'll end up with duplicates and other problems.