tags:

views:

69

answers:

1

I am having a problem saving the Parent object which contains multiple children.

The Model Classes are as follows:

public class Driver
{
   private IList<CitationEvent> _CitationEvents = new List<CitationEvent>(1);
}


public class CitationEvent
{
  public virtual Driver Driver { get; set; }
}

XML Mapping in Driver.hbm.xml

<bag name="_CitationEvents" access="field" cascade="all-delete-orphan"  inverse="true">
      <key column="DRIVER_ID" />
      <one-to-many class="CitationEvent" />
</bag>

XML Mapping in CitationEvent.hbm.xml

<many-to-one  name="Driver" class="Driver" column="DRIVER_ID" cascade="none"/>

When I try to save driver, NHibernate throws following exception "Nullable object must have a value"

The code which saves it:

ITransaction transaction = session.BeginTransaction(IsolationLevel.ReadCommitted);

try
{
  session.Save(driver);
  transaction.Commit();
}
catch (Exception ex)
{
   transaction.Rollback();
}

One more observation about the problem: When I try to just save Driver with empty list _CitationEvents it does not give me this exception.

+2  A: 

This isn't an NHibernate specific error. The probable cause is that you have a nullable property in one of your classes that you are attempting to cast to a non-nullable type. This exception is thrown by the .NET Framework when attempting to access the Value property of a nullable type that does not have a Value.

Jamie Ide