ADO.NET 实体数据模型允许循环外键引用
我在一个简单的数据库上使用新的 ADO.NET 实体数据模型。 我有一个带有主键 (PageID) 和 ParentID 外键的表/实体,该外键在 PageID 上引用自身以实现父/子“0..1 到多”关系。 在 ASP.Net 页面上,我使用带有 asp:DynamicControl 的 FormView 将其表示为控件。 除了一个关键细节之外,该部分工作正常:当页面呈现时,可能的父项列表包含其自身,并且将项目设置为具有其自身的父项不会导致错误并保存到数据库。 显然,分层对象不应该有其自身的父对象,那么我如何限制这种行为呢?
据我所知,我的选择是:
以某种方式更改 DynamicData FieldTemplates 中的foreignkey
_
edit.ascx。 这是有问题的,因为我看不到如何获取对当前实体的主键的引用,以将其从可能的外键选择中删除。 另外,这可能会影响按键发生冲突的任何非分层 fkey 引用用法。在页面上进行一些 PreRender jiggery-pokery,尝试从渲染的 DropDownList 中删除 ListItem。 这似乎不是正确的方法,因为它只修复了有问题的页面。 编辑:这就是我目前解决该问题的方法。
某种插入/更新触发器,如果与自身匹配,则强制 ParentID 为空。 这不好,因为从用户的角度来看,它会默默地失败。
有没有人有更好的方法? 另外,如果我需要提供更多详细信息,请告诉我。
-凯利
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?
As far as I can see, my options are:
Somehow change the ForeignKey
_
edit.ascx in the DynamicData FieldTemplates. This is problematic because I can't see how to get a reference to the current entity's primary key to remove it from the possible foreign key choices. Plus, this will potentially hose any non-hierarchical fkey refs usages where the keys happen to collide.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.
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.
Has anyone out there got a better way? Also, let me know if I need to provide any more detail.
-Kelly
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最终将层次结构移至装配表,因为我必须存储的不仅仅是父/子关系。 这样做还意味着我可以转储 DynamicData 控件并使用用户更直观的 TreeView 以及拖放服务器端事件 (ComponentArt)。 由于控件的性质,没有任何项目可以是它自己的父项,因此问题没有实际意义。 哇,对于我第一次使用实体框架来说,7 个月是很长的一段时间了:)
I ended up moving the hierarchy to an assembly table because I had to store more than just parent/child relationships. Doing so also meant I could dump the DynamicData control and use a more user-intuitive TreeView with drag-n-drop server-side events (ComponentArt). Because of the nature of the control, no item could be it's own parent so the problem was moot. Wow, 7 months is a long time in my first experience with Entity Framework :)