1

I would like to create a nested set model inside my database. However, I have a problem, 'cause I don't know how to start the implementation using Entity Framework Code-First.

Lets say I have such class:

public class Category
{
    public long Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Category> SubCategories { get; set; }

    // public int left { get; set; }
    // public int right { get; set; }
}

In my model I need SubCategories collection as it is right now. However I would like to implement automatic (implicit) update of left and right properties when I add/remove subcategories to/from SubCategories collection.

Is it possible?

Thank you for any answer in advance!

Best!

Lukasz Spas
  • 595
  • 2
  • 9
  • 23

1 Answers1

2

Nested sets and object graphs are two totally different models. You shouldn't store both of them in one type; that violates SRP.

Instead, put Left and Right on your code-first POCOs, load the, and then project/copy that onto objects of a different type (which are not entities) with a graph schema.

Craig Stuntz
  • 125,891
  • 12
  • 252
  • 273