I am new to nhibernate and using it firsttime with .net2.0
I am trying to do a insert .
Any help or suggestion to do it different way is a welcome.
I have two tables Employee and Emp_Relations
Employee (EmpNum(PK),Age,FirstName,LAstname)
Emp_Relations(R_ID(PK indentity),EmpNum,R_Fname)
I have two classes
public class Employee
{
#region Private
private int _empnum;
private int _age;
private Name _name ;
private IList<EmpDependent> _dependents = new List<EmpDependent>();
#endregion
#region Constructor
public Employee()
{
_name = new Name();
}
#endregion
#region Public properties
public virtual int EmpNum
{
get{return _empnum;}
set{_empnum=value;}
}
public virtual int Age
{
get{return _age;}
set{_age=value;}
}
public virtual string EmpName
{
get{return _name.CompleteName;}
}
public virtual Name Name
{
get{return _name;}
set { _name = value; }
}
public virtual void AddDependent(EmpDependent d)
{
_dependents.Add(d);
}
private IList<EmpDependent> Dependents
{
get { return _dependents; }
set { _dependents = value; }
}
#endregion
}
Second class
public class EmpDependent
{
#region Private
private int _id;
private int _empnum;
private string _fname;
#endregion
#region Constructor
public EmpDependent(string fname)
{
_fname = fname;
}
public EmpDependent()
{
}
#endregion
public virtual int ID
{
get { return _id; }
set { _id = value; }
}
public virtual string Fname
{
get{return _fname;}
set{_fname = value;}
}
public virtual int EmpNum
{
get { return _empnum; }
set { _empnum = value; }
}
}
Hibernate version:1.2.0.4000
Mapping documents:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name ="DomainModel.Employee,DomainModel" table="Employee">
<id name ="EmpNum" type ="int">
<generator class="native" />
</id>
<property name="Age" column ="age" type="int" />
<component name ="Name" class="DomainModel.Name,DomainModel">
<property name ="FirstName" column ="FirstName" type ="string" length ="50" />
<property name ="LastName" column ="LastName" type ="string" length ="50" />
</component>
<set name="Dependents" inverse="true" lazy="true">
<key column="EmpNum"/>
<one-to-many class="DomainModel.EmpDependent,DomainModel"/>
</set>
</class>
<class name="DomainModel.EmpDependent,DomainModel" table ="Emp_Relations" >
<id name="ID" type ="int" column ="R_ID">
<generator class="native"/>
</id>
<property name="Fname" type ="string" column ="R_Fname" />
<property name="EmpNum" type ="int" column ="EmpNum" />
<many-to-one name="EmpNum" class="DomainModel.Employee,DomainModel" column="EmpNum" not-null="true"/>
</class>
</hibernate-mapping>
Full stack trace of any exception that occurs:
Test method UnitTest.EmployeeRepositoryTest.AddEmployee threw exception: NHibernate.ADOException: Could not save object ---> System.InvalidCastException: Unable to cast object of type 'System.Collections.Generic.List`1[DomainModel.EmpDependent]' to type 'Iesi.Collections.Generic.ISet`1[DomainModel.EmpDependent]'..
Name and version of the database you are using:
SQlServer 2005
Thanks
|