I have a table Donations which has a CampaignID column that relates to the Campaigns Table. I need to insert a 0 in the CampaignID column instead of Null if the Campaign is
Try loading the Campaign object whose DB identity is 0 from the DB. It will then be a fully persistent object. You should then be able to set it and persist the donation.
If this works, you need to change the ID property mapping of the Campaign object. Nh is unable to determine that your transient campaign object created here :
new Campaign() {CampaignID = 0};
is actually a detached object. What you should do is add an 'unsaved value' into your mapping of say, -1. Now Nh can tell the difference between your valid detached campaign with a DB identity Id of 0, and transient new campaigns with Ids of -1. Then remember to set the Id for newly created campaigns to -1.
With this information you should understand why you have the problem.
Is the CampaignID generated with an automatic counter? Then it does not work anyway. (You can't set the id in the application.) Unless you store the Campaign with the CampaignID = 0 (see bellow).
You can make the CampaignID a nullable int, then the unsaved value is null by default.
Be aware that you get a Campaign with the CampaignID=0 in you database. This is a "Null Object". You need to store it, for instance after you setup the database.
If you want to avoid the null object, you get in troubles. NHibernate conceptually don't let you access the foreign keys, it manages it for you. You probably can do some tricks ( for instance in an interceptor), but I think it is not worth the troubles. You also have to make sure that there is no foreign key constraint, which is also not nice.