NHibernate Saving 0 to many-to-one column instead of null

前端 未结 2 1624
被撕碎了的回忆
被撕碎了的回忆 2021-01-06 16:42

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

2条回答
  •  离开以前
    2021-01-06 17:12

    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.

提交回复
热议问题