Entity Framework Code First Tree Model

DavidGouge

I have the following entity:

public class Module
{
    public Module()
    {
        SubModules = new List<Module>();
    }

    public int Id { get; set; }
    public string Title { get; set; }
    public string Action { get; set; }
    public string Controller { get; set; }
    public string Icon { get; set; }

    public List<Module> SubModules { get; set; }
}

Which, when initialised through Code First generates the following table Schema:

CREATE TABLE [dbo].[Modules](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Title] [nvarchar](max) NULL,
    [Action] [nvarchar](max) NULL,
    [Controller] [nvarchar](max) NULL,
    [Icon] [nvarchar](max) NULL,
    [Module_Id] [int] NULL,
 CONSTRAINT [PK_dbo.Modules] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF)
)

GO
ALTER TABLE [dbo].[Modules]  WITH CHECK ADD  CONSTRAINT [FK_dbo.Modules_dbo.Modules_Module_Id] FOREIGN KEY([Module_Id])
REFERENCES [dbo].[Modules] ([Id])
GO
ALTER TABLE [dbo].[Modules] CHECK CONSTRAINT [FK_dbo.Modules_dbo.Modules_Module_Id]
GO

The problem is that when I populate this table with a parent module (Module_Id of null) and two child modules (Module_Id of the parent Module) and query the DBContext's collection of Modules, I get the parent module with it's collection of child modules correctlly, but I also get the child modules returned by themselves.

So the Modules collection in the DbContext looks a bit like this:

ParentModule
---Child Module
---Child Module
Child Module
Child Module

What I need is for those two Child Modules to not be returned as Modules in their own right, but only as the children of the parent.

Hope I've explained this ok.

Kristof Claes

I would add a ParentModuleId property (int?) to your Module class.

public class Module
{
    public Module()
    {
        SubModules = new List<Module>();
    }

    public int Id { get; set; }
    public string Title { get; set; }
    public string Action { get; set; }
    public string Controller { get; set; }
    public string Icon { get; set; }

    public int? ParentModuleId { get; set; }

    [ForeignKey("ParentModuleId")]
    public virtual ICollection<Module> SubModules { get; set; }
}

Notice how I have also added a ForeignKey attribute to the SubModules list to ensure that the new ParentModuleId property is used as the foreign key column.

This way you can manually check for the presence of a parent module.

You can then get the "root modules" like this:

var rootModules = context.Modules.Where(x => x.ParentModuleId == null);

If you need that a lot, you can also create an extension method:

public IQueryable<Module> WithoutParent(this IQueryable<Module> modules)
{
    return modules.Where(x => x.ParentModuleId == null);
}

var rootModules = context.Modules.WithoutParent();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Code first Entity Framework model in PHP

From Dev

Switching from entity framework model first to code first

From Dev

Entity Framework 6 Code First - Comments on a Tree Structure

From Dev

How to store a binary tree as a property of an entity framework object (code first)

From Dev

Error in Seed Method of Entity Framework, Tables not created in Code First Model

From Dev

Entity Framework 5 code first cannot get model to work

From Dev

Entity Framework 5 code first cannot get model to work

From Dev

Entity Framework 6 Code First: specific model design implementation

From Dev

BaseEntity in Entity Framework Model First

From Dev

Best way to use Entity Framework (Database First , Model First , Code First)

From Dev

WCF with Entity Framework Code First

From Dev

Entity Framework Inheritance Code First

From Dev

Relationships in Entity Framework Code First

From Dev

Entity Framework Code First connection

From Dev

Entity Framework Code First GenericTypeArguments

From Dev

WCF with Entity Framework Code First

From Dev

Entity Framework - Code First - Relationship

From Dev

Entity Framework Code First - Relationships

From Dev

How to change from Model-first to Code-First in Entity Framework

From Dev

Using database first entity with code first entity in entity framework

From Dev

Entity Framework 6 Model First Migration

From Dev

Entity Framework Model First Self referencing

From Dev

Entity Framework model first connection string

From Dev

Entity Framework Model First 1:0..1 Relationship

From Dev

Entity Framework Model First Self referencing

From Dev

C# Model User, Friend Requests and Friends with Entity Framework Code First

From Dev

How to use Entity Framework code-first with a newer database model version

From Dev

Team Environment Entity Framework Code First - Is it appropriate to just delete _MigrationHistory table to resync your model project to the database?

From Dev

How to Keep Entity Framework Code First from using Derived Classes that Aren't Part of the Model

Related Related

  1. 1

    Code first Entity Framework model in PHP

  2. 2

    Switching from entity framework model first to code first

  3. 3

    Entity Framework 6 Code First - Comments on a Tree Structure

  4. 4

    How to store a binary tree as a property of an entity framework object (code first)

  5. 5

    Error in Seed Method of Entity Framework, Tables not created in Code First Model

  6. 6

    Entity Framework 5 code first cannot get model to work

  7. 7

    Entity Framework 5 code first cannot get model to work

  8. 8

    Entity Framework 6 Code First: specific model design implementation

  9. 9

    BaseEntity in Entity Framework Model First

  10. 10

    Best way to use Entity Framework (Database First , Model First , Code First)

  11. 11

    WCF with Entity Framework Code First

  12. 12

    Entity Framework Inheritance Code First

  13. 13

    Relationships in Entity Framework Code First

  14. 14

    Entity Framework Code First connection

  15. 15

    Entity Framework Code First GenericTypeArguments

  16. 16

    WCF with Entity Framework Code First

  17. 17

    Entity Framework - Code First - Relationship

  18. 18

    Entity Framework Code First - Relationships

  19. 19

    How to change from Model-first to Code-First in Entity Framework

  20. 20

    Using database first entity with code first entity in entity framework

  21. 21

    Entity Framework 6 Model First Migration

  22. 22

    Entity Framework Model First Self referencing

  23. 23

    Entity Framework model first connection string

  24. 24

    Entity Framework Model First 1:0..1 Relationship

  25. 25

    Entity Framework Model First Self referencing

  26. 26

    C# Model User, Friend Requests and Friends with Entity Framework Code First

  27. 27

    How to use Entity Framework code-first with a newer database model version

  28. 28

    Team Environment Entity Framework Code First - Is it appropriate to just delete _MigrationHistory table to resync your model project to the database?

  29. 29

    How to Keep Entity Framework Code First from using Derived Classes that Aren't Part of the Model

HotTag

Archive