Solved

MVC Code-First migration scaffold self-referential model

Posted on 2013-01-26
5
656 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
[X]
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
  • 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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

729 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