• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 668
  • Last Modified:

MVC Code-First migration scaffold self-referential model

I have a constructed a model using code first in C#. The model literally represents a container element for a website building application, in other words, the model defines a Div tag or some such HTML element. Like a Div tag which can contain multiple child elements, I have tried to represent this in my model, but the scaffolding to the DB, does not give me what I'd expect.

I should get a new many to many joins table, but instead I only get a single column in the DB which expects a single int data type.

Here is the model:

public class ElementContainer
        public int ElementContainerID { get; set; }
        public int PageId { get; set; }
        public int? ParentElementContainerID { get; set; }
        public string ElementContainerName { get; set; }
        public ElementType ElementType { get; set; }
        public string ElementClass { get; set; }

        public List<ElementContainer> NestedContainers { get; set; }

Open in new window

The last line is the self-referential attribute which just appears as a column called ElementContainer_ElementContainerID

Thanks in advance!
  • 2
  • 2
1 Solution
Craig WagnerSoftware ArchitectCommented:
From your description I would say that EF is generating exactly what I would expect. Your model is describing a simple hierarchical relationship where one ElementContainer can have many children. That can be represented in a single self-referencing table.

If you truly want a many-to-many relationship you'll need to have a second entity in your model, one that contains a ParentElementContainerId and a ChildElementContainerId.

Having said that, given that you're trying to model HTML I don't think a many-to-many is necessary. Each parent (e.g. a <div>) can have many children, but each child can only have one parent. I think your current model (and the resulting database) are correctly modeled.
juliandormonAuthor Commented:
Ah ok. Sometimes I over-complicate things. Thanks so much.
juliandormonAuthor Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for juliandormon's comment #a38827034

for the following reason:

Good straight forward answer.
Craig WagnerSoftware ArchitectCommented:
I'm assuming the poster meant my answer was a "Good straight forward answer" so I'm not sure why there is a close request pending. I feel I should get the points for answering the question correctly.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now