I have a code that was working perfectly on NHibernate 3.1, but when it is not working on NHibernate 4.0
So, this is the class relations
public class Employee : BaseEntity
{
...
public Department Dept { get; set; }
}
public class Department : BaseEntity
{
...
public IList<Employee> Employees { get; set; }
}
and for the mapping we have this
DepartmentMap : ClassMap<Department>
{
Table("....");
HasMany(x => x.Employees).KeyColumn("DeptId").Not.KeyNullable();
}
EmployeeMap : ClassMap<Employee>
{
Reference(x => x.Dept).Column("DeptId");
}
and when I am adding an employee like that
var dept = session.Load<Department>(deptId);
newEmployee.Dept = dept;
session.Save(newEmployee);
But it is throwing an error:
NHibernate.PropertyValueException: not-null property references a null or transient value
I read that I have to add the relation in two ways, so I modified it to this
var dept = session.Load<Department>(deptId);
newEmployee.Dept = dept;
dept.Employees.Add(newEmployee);
session.Save(newEmployee);
But now I have this error :
NHibernate.PropertyValueException: Error dehydrating property value for... System.IndexOutOfRangeException: Invalid index 7 for this SqlParameterCollection with Count=7.
So, I want to know how to fix it and where I can read about the changes in NHibernate about this with bi-direction