Lukasz Spas
Lukasz Spas

Reputation: 605

EF Code-First and nested sets

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!

Upvotes: 1

Views: 1851

Answers (1)

Craig Stuntz
Craig Stuntz

Reputation: 126557

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.

Upvotes: 2

Related Questions