Solved

MVC Code-First migration scaffold self-referential model

Posted on 2013-01-26
5
653 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.NET (VB) return a record 2 52
Need syntax help building hyperlink 2 41
Aspx calendar pop up control 3 28
How to set focus on a dynamic control 18 27
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

726 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