title: ADO.NET Entity Data Model Allows Circular Foreign Key Refs
body: <p>I am using the new ADO.NET Entity Data Model on a simple database.  I have a table/entity with a primary key (PageID) and a ParentID foreign key that references back to itself on  PageID for a parent/child "0..1 to many" relationship.  On a ASP.Net page, I'm using a FormView with a asp:DynamicControl to express this as a control.  That part works fine except for one crucial detail:  When the page renders, the list of possible parents includes itself, and setting an item to have a parent of itself causes no errors and saves to the database.  Obviously a hierarchical object isn't supposed to have a parent of itself, so how do I restrict this behavior?</p>
  
  <p>As far as I can see, my options are:</p>
  
  <ul>
  <li><p>Somehow change the ForeignKey<code>_</code>edit.ascx in the DynamicData FieldTemplates.  This is problematic because I can't see how to get a reference to the current entity's <em>primary</em> key to remove it from the possible <em>foreign</em> key choices.  Plus, this will potentially hose any non-hierarchical fkey refs usages where the keys happen to collide.</p></li>
  <li><p>Do some PreRender jiggery-pokery on the page to try to remove the ListItem from the rendered DropDownList.  This doesn't seem like the Right Way as it only fixes it for the page in question.  EDIT:  This is how I'm solving it currently.</p></li>
  <li><p>Some sort of insert/update trigger to force the ParentID to null if it matches itself.  This isn't good because it fails silently from the user's perspective.</p></li>
  </ul>
  
  <p>Has anyone out there got a better way?  Also, let me know if I need to provide any more detail.</p>
  
  <p>-Kelly</p>