Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

MVC Code-First migration scaffold self-referential model

Posted on 2013-01-26
5
652 Views
Last Modified: 2013-01-28
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!
0
Comment
Question by:juliandormon
  • 2
  • 2
5 Comments
 
LVL 21

Accepted Solution

by:
Craig Wagner earned 500 total points
ID: 38824219
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.
0
 

Author Comment

by:juliandormon
ID: 38827034
Ah ok. Sometimes I over-complicate things. Thanks so much.
0
 

Author Comment

by:juliandormon
ID: 38827187
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.
0
 
LVL 21

Expert Comment

by:Craig Wagner
ID: 38827188
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.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question