I have a Companies table mapped to a Company class and a Websites table mapped to a Website class. My Companies table is basically non-moving, however I'm trying to insert a new Website record with the following:
var website = new Website
{
Active = true,
SiteRoot = "example.com",
CompanyId = 100
};
var repo = new WebsiteRepository();
repo.Add(website);
The website class has a CompanyId property which is what maps back to the ID column in the Companies table, but because the Website class has a many-to-one mapping, it's trying to insert the website.Company.Id
property into the table(which is null) instead of taking the website.Id
property.
There are no foreign keys involved as the tables are in 2 separate databases.
This is the error I'm receiving:
NHibernate.Exceptions.GenericADOException: could not insert: [Models.Website][SQL: INSERT INTO Database1.dbo.Websites (CompanyID, SiteRoot, Active) VALUES (?, ?, ?); select SCOPE_IDENTITY()] ---> System.Data.SqlClient.SqlException: Cannot insert the value NULL into column 'CompanyID', table 'Database1.dbo.Websites'; column does not allow nulls. INSERT fails.
NHibernate: INSERT INTO Database1.dbo.Websites (CompanyID, SiteRoot, Active) VALUES (@p0, @p1, @p2); select SCOPE_IDENTITY();@p0 = NULL [Type: Int32 (0)], @p1 = 'example.com' [Type: String (4000)], @p2 = True [Type: Boolean (0)]
Here are the mappings:
<hibernate-mapping assembly="Models" namespace="Models" xmlns="urn:nhibernate-mapping-2.2" schema="Database1.dbo">
<class name="Website" table="Websites" lazy="true" >
<id name="Id" column="ID">
<generator class="identity" />
</id>
<many-to-one name="Company" column="CompanyID" fetch="join"/>
<property name="SiteRoot">
<column name="SiteRoot" sql-type="nvarchar" length="50" not-null="false" />
</property>
<property name="Active">
<column name="Active" sql-type="bit" not-null="true" />
</property>
</class>
</hibernate-mapping>
<hibernate-mapping assembly="Models" namespace="Models" xmlns="urn:nhibernate-mapping-2.2" schema="Database2.dbo">
<class name="Company" table="Companies" lazy="true" >
<id name="Id" column="ID">
<generator class="identity" />
</id>
<property name="CompanyId">
<column name="CompanyID" sql-type="int" not-null="true" />
</property>
<property name="CompanyName">
<column name="CompanyName" sql-type="nchar" not-null="false" />
</property>
<set name="Websites" table="Websites" schema="Database1.dbo" lazy="false">
<key column="CID"/>
<one-to-many class="Models.Website, Models" />
</set>
</class>
</hibernate-mapping>
CompanyId
property? It is not mapped to the database. There is most probably something wrong in the way you handle references. – Stefan Steinegger