The main question . How to create a one-to-many bidirectional map in Fluent NHibernate?
Details:
I have a parent with a lot of children. In my case, it makes no sense for a child not to have a parent, so in the database I would like the foreign key for the parent to have a NOT NULL constraint. I automatically generate my database from the Fluent NHibernate mapping.
I have a parent with many children:
public class Summary { public int id {get; protected set;} public IList<Detail> Details {get; protected set;} } public class Detail { public int id {get; protected set;} public string ItemName {get; set;}
Here is the mapping I started with:
public class SummaryMap : ClassMap<Summary> { public SummaryMap() { Id(x => x.ID); HasMany<Detail>(x => x.Details); } } public class DetailMap : ClassMap<Detail> { public DetailMap() { Id(x => x.ID); Map(x => x.ItemName).CanNotBeNull(); } }
In the Detail table, Summary_id should be Not Null, because in my case it makes no sense to have a Detail object that is not tied to the resulting object. However, only the use of the HasMany () card leaves the understated Summary_id key invalid.
I found in NHibernate docs ( http://www.hibernate.org/hib_docs/nhibernate/html/collections.html ): "If a parent element is required, use a one-to-many bidirectional relationship."
So, how do I create a one-to-many bidirectional map in Fluent NHibernate?
nhibernate fluent-nhibernate nhibernate-mapping
Nathan Nov 22 '08 at 0:42 2008-11-22 00:42
source share