0

I'm doing some research into which of Entity Framework and (fluent) nHibernate is the best fit for a project. And I've stumbled into some mapping problems. I'm hoping someone here can help me out with a quick solution to my problem :)

Here are my entity classes:

public abstract class DisplayItem
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual string Description { get; set; }
}

public class Link : DisplayItem
{
    public virtual Node FromNode { get; set; }
    public virtual Node ToNode { get; set; }

    public Link()
    {
    }
}

public class Node : DisplayItem
{
    public virtual IList<Link> LinksFrom { get; set; }
    public virtual IList<Link> LinksTo { get; set; }

    public Node()
    {
        LinksFrom = new List<Link>();
        LinksTo = new List<Link>();
    }
}

I tried first to use the automatic mapper, but that did not work at all. It did create the database and saved data, but the data was not connected correctly. I think the problem comes because a Link contains two instances of Node

I then tried manually mapping it, here is the code I used:


public class LinkMap : ClassMap
{
    public LinkMap()
    {
        Id(x => x.Id);
        Map(x => x.Name);
        Map(x => x.Description);
        References(x => x.FromNode);
        References(x => x.ToNode);
    }

}

class NodeMap : ClassMap
{
    public NodeMap()
    {
        Id(x => x.Id);
        Map(x => x.Name);
        Map(x => x.Description);
        HasMany(x => x.LinksFrom)
            .Cascade.All();
        HasMany(x => x.LinksTo)
            .Cascade.All();
    }
}

Any Idea what I'm doing wrong here?

Thomas Winsnes
  • 1,961
  • 1
  • 13
  • 15
  • I had a guess at the answer. If its not correct, what do you mean by the data is not connected properly? And what inheritance do you want to use? – Iain Nov 04 '10 at 23:28

1 Answers1

1

Firstly, use generics for your lists:

public class Node : DisplayItem
{
    public virtual IList<Link> LinksFrom { get; set; }
    public virtual IList<Link> LinksTo { get; set; }

    public Node()
    {
        LinksFrom = new List<Link>();
        LinksTo = new List<Link>();
    }
}

Secondly, you have inheritance, but you have not stated what style you what to do in the database. If you want to ignore the inheritence, then you need to do this Ignoring base-types:

AutoMap.AssemblyOf<Entity>(cfg)
  .IgnoreBase<Entity>();

The default is table-per-subclass.

If you do a table-per-class-hierarchy, be careful of this.

Community
  • 1
  • 1
Iain
  • 10,814
  • 3
  • 36
  • 31