nhibernatenhibernate-mapping-by-code

Mapping a collection of Interface with NHibernate Mapping ByCode


With FluentNHibernate I mapped a collection of interface by specifying the concrete type in the mapping class. I'm trying to convert to Maping.ByCode.

Entity classes:

public class Parent Entity
{
public virtual Guid Id{get;set;}
public virtual IList<IChildEntity> Children{get;set;}
}

public class ChilEntity:IChildEntity
{
public virtual Guid Id{get;set;}
}

With FluentNHibernate:

public class ParentEntityMap:ClassMap<ParentEntity>
{
public ParentEntityMap()
{
Table("ParentEntity");
Id(x => x.Id);
HasMany<ChildEntity>(x=>x.Children)
 .KeyColumn("Parent");
}
}

With Mapping ByCode:

public class ParentEntityMap:ClassMapping<ParentEntity>
{
 Public ParentEntityMap()
 {
  Table("ParentEntity");
  Id(x=>x.Id);
  Bag<ChildEntity>(x=>(IList<ChildEntity>)x.Children,
   m=>m.Key(k=>k.Column("Parent")),
   ce=>ce.OneToMany()
  );

The mapping ByCode doesn't work. Is there a way to achieve what is done with Fluent NHibernate?


Solution

  • Try setting Class:

    Bag(x=>x.Children,
       m=> m.Key(k=>k.Column("Parent")),
       ce=> ce.OneToMany(m => m.Class(typeof(ChildEntity)))
      );