I'm trying to map a parent-child relationship using NHibernate (2.1.2), MySql.Data (6.2.2) and MySQL Server (5.1). I figured out that this must be done using a <bag>
in the mapping file. I build a test app which is running without yielding any errors and is doing an insert
for each entry but somehow the foreign key inside the children table (ParentId) is always empty (null).
Here are the important parts of my code...
Parent
public class Parent
{
public virtual int Id { get; set; }
public virtual IList<Child> Children { get; set; }
}
<class name="Parent">
<id name="Id">
<generator class="native"/>
</id>
<bag name="Children" cascade="all">
<key column="ParentId"/>
<one-to-many class="Child"/>
</bag>
</class>
Child
public class Child
{
public virtual int Id { get; set; }
}
<class name="Child">
<id name="Id">
<generator class="native"/>
</id>
</class>
Program
using (ISession session = sessionFactory.OpenSession())
{
session.Save(
new Parent()
{
Children = new List<Child>()
{
new Child(),
new Child()
}
});
}
Any ideas what I did wrong?