Entity Data ADO.NET allows circular foreign keys

I am using the new ADO.NET Entity Data Model in a simple database. I have a table / entity with a primary key (PageID) and a foreign key ParentID that referencing itself to the PageID for a 0..1 to many parent / child relationship. In an ASP.Net page, I am using a FormView with asp: DynamicControl to express this as a control. This part works great, except for one important detail: when the page is rendered, the list of possible parents includes itself, and setting the element to the parent itself is error-free and saves the database. Obviously, a hierarchical object should not have a parent, so how do you restrict this behavior?

As far as I can tell, my options are:

  • Change ForeignKey _

    edit.ascx in DynamicData FieldTemplates somehow. This is problematic because I don't see how to get a reference to the current primary key of an entity in order to remove it from possible foreign key choices. Also, this would potentially lead to any non-hierarchical fkey refs actions where keys might collide.

  • Pre-build PreRender jiggery-pokery on the page to try and remove the ListItem from the rendered DropDownList. This doesn't seem like the correct path as it only commits it to the page in question. EDIT: This is how I currently solve it.

  • Some sort of insert / update trigger to force the ParentID to null if it matches itself. This is not good because it fails from the user's point of view.

Does anyone have a better way? Also, let me know if I need to provide more details.

-Kelly

+1


source to share


1 answer


I moved the hierarchy to the assembly table because I had to store more than just parent-child relationships. This also meant that I could reset the DynamicData control and use a more intuitive TreeView with drag-n-drop server side events (ComponentArt). Due to the nature of the control, no element could be its own parent so the issue was moot. Wow, 7 months is a long time in my first experience with Entity Framework :)



+1


source







All Articles