Firstly, don't map the foreign-key id as a property on Child; that way pain lies. Secondly, you need to set the Parent property yourself before save. The recommended practice for this is to create an AddChild method on your Parent class that adds the Child into the collection and at the same time sets the Parent property.
On Thu, Feb 4, 2010 at 12:45 AM, serhatozgel <[email protected]> wrote: > Hi, > > I have a class, with a property for its children: > > public class Parent > { > public virtual long Id { get; set; } > public virtual IList<Child> Children { get; set; } > } > > public class Child > { > public virtual long Id { get; set; } > public virtual long ParentId { get; set; } > public virtual Parent Parent { get; set; } > } > > public class ParentMap : ClassMap<Parent> > { > public ParentMap() > { > HasMany(p => p.Children) > .KeyColumn("ParentID") > .Table("Child") > .Cascade.AllDeleteOrphan() > .Inverse() > .Not.LazyLoad(); > } > } > > public class ChildMap : ClassMap<Child> > { > public ChildMap() > { > Map(c => c.ParentId); > > References(c => c.Parent) > .Column("ParentID") > .Not.LazyLoad() > .Not.Insert(); > } > } > > I am trying to insert a new parent: > > Parent parent = new Parent() > { > Childrent = new List<Child>() > { > new Child(); > } > }; > > session.SaveOrUpdate(parent); > > which gives me: > > Cannot insert the value NULL into column 'ParentID', table > 'companydb.dbo.Children'; column does not allow nulls. INSERT fails. > The statement has been terminated. > > Updating the parent works fine and the children are correctly updated. > But I cannot manage to insert a new parent with its children. What is > the correct mapping for that? > > -- > You received this message because you are subscribed to the Google Groups > "Fluent NHibernate" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]<fluent-nhibernate%[email protected]> > . > For more options, visit this group at > http://groups.google.com/group/fluent-nhibernate?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Fluent NHibernate" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/fluent-nhibernate?hl=en.
