Note that there are some explanatory texts on larger screens.

plurals
  1. POFluent NHibernate HasMany Foreign Key Mapping Problem
    text
    copied!<p>I'm trying to map a simple data structure in nhibernate</p> <p><strong>Tables:</strong></p> <pre><code>Employees employeeID int username varchar(30) departmentID int Departments departmentID int deptName varchar(50) </code></pre> <p>My department mapping is like so:</p> <pre><code>public class DepartmentMap: ClassMap&lt;Department&gt; { public DepartmentMap() { Id(m =&gt; m.DepartmentID).Column("departmentID"); Map(m =&gt; m.DepartmentName).Column("deptName").Length(50); HasMany(m =&gt; m.Employees); Table("Departments"); } } </code></pre> <p>... and the employee mapping</p> <pre><code>public class EmployeeMap : ClassMap&lt;Employee&gt; { public EmployeeMap() { Id(x =&gt; x.EmployeeID, "employeeID"); Map(x =&gt; x.UserName, "username").Length(30); References&lt;Department&gt;(x =&gt; x.Department, "departmentID"); Table("Employees"); } } </code></pre> <p>I'm trying to loop through departments and pull all employees from each department:</p> <pre><code>foreach (var i in _directoryData.DepartmentCollection) { foreach (var e in i.Employees) { Debug.WriteLine(i.DepartmentName + " " + e.UserName); } } </code></pre> <p>which gives me an error stating "<code>Invalid column name 'Department_id'.</code>" ... and in the generated query it uses department_id as well. When I just loop through departments and output the department name it works fine.</p> <p>Any idea what I'm missing to get the correct column name for departmentID? Here are my model objects just in case:</p> <pre><code>public class Department { public virtual int DepartmentID { get; set; } public virtual string DepartmentName { get; set; } public virtual ICollection&lt;Employee&gt; Employees { get; set; } } public class Employee : PersistentEntity { public virtual int EmployeeID { get; set; } public virtual string UserName { get; set; } public virtual Department Department { get; set; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload