views:

18

answers:

1

Hello,

I have a situation where I need to store a graph data structure in the database.

This means an entity can have unlimited number of related entities of the same type (related entity can have unlimited related entities as well).

I was thinking that many-to-many relationship would solve my problem.

I'm trying to do mapping with FluentNHibernate, but following code does not generate SQL for CREATE TABLE for EntityEntity table:

HasManyToMany(x => x.RelatedEntities).ChildKeyColumn("RelateEntityID").ParentKeyColumn("EntityID"); 

Am I doing something wrong? Or should I create a separate entity and do mapping using HasMany()?

Thank You very much!

+1  A: 

Hi Daniil

Regarding to your question I will create an entity which has one parent and a list of children.

here is my sample:

public class GrafNode : AdvanceEntity
    {
        public GrafNode()
        {
            this.Children = new List<GrafNode>();
        }

        public virtual string Name  { get; set; }

        public virtual GrafNode Parent { get; set; }

        public virtual IList<GrafNode> Children { get; private set; }

        public virtual void AddChild(GrafNode node)
        {
            node.Parent = this;
            this.Children.Add(node);
        }
    }

and the mapping override class is:

public class GrafNodeMappingOverride : IAutoMappingOverride<GrafNode>
{
    public void Override(AutoMapping<GrafNode> mapping)
    {
        mapping.HasOne(x => x.Parent);
        mapping.HasMany(x => x.Children).KeyColumn("parentId");
    }
}

as you can see one node could have a parent and a list of children.

and here is my table structure, please ignore DeletedBy and DeletedDate (ower AdvanceEntity is a soft deletable entity)

alt text

If you need some unittests to see how it work let me know.

isuruceanu
Thank You. It got me many ideas :)
Daniil Harik