为自引用关系中的所有子项更新字段
大家好,
我正在使用实体框架和 ASP.NET MVC 3。在应用程序的一部分中,我在表中具有自引用关系(ID、ParentID 等...)该表还有一个 Status 字段。当用户更新状态时,它需要更新其所有子级的状态。深度是可变的。
我希望在不使用 Load() 函数的情况下执行此操作,因为我听说这效率不高。我还遇到了一种使用存储过程来完成此操作的方法,但我希望此功能在 EF 中完成,最好仅在 POST 操作中完成。
HI all,
I am using Entity Framework and ASP.NET MVC 3. In one part of the app, I have a self -referencing relationship in a table (ID, ParentID, etc...) This table also has a Status field. When a user updates a Status, it needs to update the statuses of all of it's children. The depth is variable.
I'm looking to do this without using the Load() function as I heard that is not efficient. I also came across a way to do it with a stored procedure, but I want this functionality to be done in EF and ideally just in the POST action.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不幸的是,实体框架在自引用关系方面非常薄弱。如果层次结构的可能级别数量有限,您可以采取一些技巧,但仅使用存储过程可能会更好。
Unfortunately, Entity Framework is very weak on self-referencing relationships. There are some tricks you can do if there are a finite number of possible levels to your hierarchy, but you'll probably be better off just using a Stored Procedure.
答案在这篇文章中解决了:
更改在树结构中级联到子级 ASP.NET MVC 3
它是为了创建一个递归方法 - 即只是一个再次调用自身的方法。
Answer is solved in this post:
Changes Cascading down to children in a Tree structure ASP.NET MVC 3
And it was to make a recursive method - i.e. just a method that calls itself again.