?
Solved

MVC Code-First migration scaffold self-referential model

Posted on 2013-01-26
5
Medium Priority
?
658 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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

765 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