Mapping an instance of IList in NHibernate

Posted by Martin Kirsche on Stack Overflow See other posts from Stack Overflow or by Martin Kirsche
Published on 2010-04-06T16:12:16Z Indexed on 2010/04/06 16:23 UTC
Read the original article Hit count: 256

Filed under:
|
|
|

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?

© Stack Overflow or respective owner

Related posts about nhibernate

Related posts about mapping