Hi,
I am using nhibernate to insert data into a table but when it comes to relation tables and inserting into a child table I always get the error that the foreign key is always null. I am pretty sure I am doing it right and I know the id is not null when it passed over.
My parent map look like this.
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="eImage2" assembly="eImage2" >
<class name="nUsers" table="Users" schema="dbo">
<id name="Id" column="UserID" type="Int32">
<generator class="identity"/>
</id>
<!-- one-to-one mapping: Types-->
<many-to-one name="Type" class="nTypes" column="TypeID" cascade="none" />
<!-- A cat has to have a name, but it shouldn' be too long. -->
<property name="Username" column="Username" type="string"/>
<property name="Password" column="Password" type="string"/>
<property name="Forename" column="Forename" type="string"/>
<property name="Surname" column="Surname" type="string"/>
<property name="TypeId" column="TypeID" type="Int32"/>
<property name="Created" column="Created" type="Date"/>
<bag name="Files" table="Files" cascade="all" lazy="false">
<key column="UserID"/>
<one-to-many class="nFiles" />
</bag>
</class>
</hibernate-mapping>
My child map looks like this:
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="eImage2" assembly="eImage2" >
<class name="nFiles" table="Files" schema="dbo">
<id name="Id" column="FileID" type="Int32">
<generator class="identity"/>
</id>
<!-- one-to-one mapping: Customer -->
<many-to-one name="User" class="nUsers" column="UserID" cascade="none"/>
<!-- A cat has to have a name, but it shouldn' be too long. -->
<property name="FileName" column="FileName" type="string"/>
<property name="FileType" column="FileType" type="string"/>
<property name="FileSize" column="FileSize" type="string"/>
<property name="IPAddress" column="IPAddress" type="string"/>
<property name="Approved" column="Approved" type="boolean"/>
</class>
</hibernate-mapping>
The code that I call looks like this:
Code:
public void dbFileInsert(int id, string fileName, int fileSize, string fileType, string ip, out int status)
{
// Get the Session from NHibernateHttpModule
int result = 0;
try
{
ISession session = NHibernateControl.GetCurrentSession();
ITransaction tx = session.BeginTransaction();
nUsers u2 = session.Get(id);
nFiles f2 = new nFiles();
f2.FileName = fileName;
f2.FileType = fileType;
f2.FileSize = fileSize;
f2.IPAddress = ip;
f2.Approved = false;
u2.Files.Add(f2);
session.Save(u2);
session.Flush();
tx.Commit();
}
catch (HibernateException ex)
{
NHibernateControl.CloseSession();
setMsg(ex.Message);
result = -1;
}
catch (Exception ex)
{
NHibernateControl.CloseSession();
setMsg(ex.InnerException.Message);
result = -1;
}
NHibernateControl.CloseSession();
status = result;
}
The error I keep recieving is:
Error : Cannot insert the value NULL into column 'UserID', table 'C:\DOCUMENTS AND SETTINGS\ADC060\MY DOCUMENTS\VISUAL STUDIO 2008\PROJECTS\EIMAGE2\EIMAGE2\APP_DATA\EIMAGE2.MDF.dbo.Files'; column does not allow nulls. INSERT fails. The statement has been terminated.
I am guessing it is either to do with my map or to do with the way I am trying to insert the new data. My tables do not allow nulls for the primary or foreign key in either map.
Thanks inadvance.