FluentNHibernate: how to map elements of a list

19 Views Asked by At

I have the following classes:

public abstract class AbstractGeometry : IGeometry
{
    // something
}

public class CompositeGeometry : AbstractGeometry
{
    IGeometry MainGeometry { get; set; }
    IList<IGeometry> Geometries { get; set; }
}

So the CompositeGeometry has a MainGeometry and a list of geometries. Any given geometry may belong to multiple CompositeGeometries.

I have a mapping class for AbstractGeometry and another mapping class for CompositeGeometry.

Now I was wondering: Is it possible to handle the mapping for the list of Geometries in the mapping class of CompositeGeometry?

In the database I already created three tables: AbstractGeometry, CompositeGeometry and CompositeGeometryElements.

The CompositeGeometry table contains a AbstractGeometryId and a MainGeometryId.

The CompositeGeometryElements table contains a CompositeGeometryId and a AbstractGeometryId.

1

There are 1 best solutions below

0
Firo On
public class AbstractGeometryMap : ClassMap<CompositeGeometry>
{
    public AbstractGeometryMap()
    {
        Id(x => x.Id);
    }
}

public class CompositeGeometryMap : SubclassMap<CompositeGeometry>
{
    public CompositeGeometryMap()
    {
        References(x => x.MainGeometry, "MainGeometryId");
        HasManyToMany(x => x.Geometries)
            .Table("CompositeGeometryElements")
            .ParentKeyColumn("CompositeGeometryId")
            .ChildKeyColumn("CompositeGeometryId");
    }
}