tags:

views:

951

answers:

1

NHibernate is driving me 'Nuts'!!!

I have the following mapping. Which returns this error when I try to add a record to the DB. "Unknown entity class: System.Int64"

 <class name="CalConnector" table="tb_calConnectors" lazy="false" >
<id name="id" column="id">
  <generator class="hilo"/>
</id>
<many-to-one name="calendarID" class="Calendar" lazy="false" cascade="save-update">
  <column name="calendarID" sql-type="bigint" not-null="false"/>
</many-to-one>

I set the mapping up like this so when I generate the schema using the mapping files this foreign key is setup; CONSTRAINT [FK94A85A28236EBB2B] FOREIGN KEY ([calendarID]) REFERENCES [dbo].[tb_calendars] ([id])

I generated the schema and everything was great. Now when I try and add a CalConnector with a reference to a existing calendarID I get an error. If I take out the many-to-one relationship; ie:

<property name="calendarID"/>

I can create the record but when NHibernate generates the schema the foreign key is not set.

It's taking me hours and hours just to set up some simple CRUD stuff. I'm hoping that NHibernate will give me some payback for all the time it takes to get right.

If anyone can spot the problem, please give me a hint then I can get this done and have a deserved beer. Any help much *2 appreciated.

+1  A: 

Hi, I won't recommend you puting calendarId as the name in the many-to-many tag, the name property must be the name of the property that holds the calendar, i.e. name="Calendar". The other part seems ok, but I don't think as well that specifying the sql-type helps at all, this prevents database agnostism because overrides hbm2ddl type inflection. So you can try the following mapping:

<class name="CalConnector" table="tb_calConnectors" lazy="false" >
  <id name="id" column="id">
    <generator class="hilo"/>
  </id>
  <many-to-one name="Calendar" class="Calendar" lazy="false" cascade="save-update" column="calendarId" not-null="false" />
</class>

Remember that the CalConnector must have a virtual property called Calendar of type Calendar to use this mapping.

Marc Climent