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?