Question

I want to design a base tree node class from which other entity classes can derive, I thought it would be like this:

public abstract class BaseTreeNode
{
  public string Id{get;set;}

  public string Name{get;set;}

  public string ParentId{get;set;}

  [ForeignKey("ParentId")]
  public BaseTreeNode ParentNode{get;set;}
}

public class MenuNode:BaseTreeNode
{

}

However, it doesn't seem to work when try to build some hierarchies with MenuNode class. It threw a SqlException:Invalid object name 'dbo.BaseTreeNode' when I called SaveChanges method.

I thought the problem was about the property 'ParentNode' as it's an abstract class. So, how should I design my classes in this case with entity framework 4.1 code first.

Many thanks!

Was it helpful?

Solution

The problem with this approach will be performance. You may want to store multiple types of trees in the DB (Menu/SubMenu, Category/Subcategory), but this type of inheritance will cause all of them to be stored together in a single table. See this question: What are the pros and cons of one master base table which all entities inherit from?

The question is whether it will ever be necessary to query all types of nodes, regardless of the type of tree.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top