Smooth nhibernate one-to-many component

I have a couple of classes and you want to map them correctly to the database:

public class A { public virtual Guid Id { get; private set; } public virtual ComponentClass Component { get; set; } } public class ComponentClass { public virtual IList<B> Elements { get;set; } } public class B { public virtual Guid Id { get; private set; } public virtual DateTime Time { get; set; } } 

I match them using smooth comparisons:

 public class AMap : ClassMap<A> { public A() { Id(x => x.Id); Component(x => x.Component, c => c.HasMany(x => x.Elements).Inverse().Cascade.All()); } } public class BMap : ClassMap<B> { public B() { Id(x => x.Id); Map(x => x.Time); } } 

When I save my entity, I have class A mapped to one table and class B to another, as expected. But I have zeros in the Component_id column. Can you tell me what I am missing here?

0
source share
3 answers

I believe that the Components should be in the same table, as clearly indicated in the Ayende blog post , since they serve only to make the data better represented as an object model. Be sure to read his blog, this is probably one of nHibernate's best resources.

+1
source

Ok, I solved my problem - I can use the id of my parent class. Thus, the display of components will be as follows:

 public class AMap : ClassMap<A> { public A() { Id(x => x.Id); Component(x => x.Component, c => c.HasMany(x => x.Elements).Cascade.All().Column("Id")); } } 

So obvious how I look at it now ... but it took me an hour.

+1
source

If you have a one-to-many association directly related to a set of components (that is, without the ComponentClass shell according to the question), you can directly map it:

 HasMany(x => x.Elements) .AsSet() .Table("ElementTable") .KeyColumn("KeyColumn") .Cascade.All() .Component(x => { x.Map(c => c.Id); x.Map(c => c.Time); }) .LazyLoad(); 
+1
source

All Articles