带查询的 EF CTP5 自引用模型
我有一个带有自引用子属性的 POCO Menu 类:
public class Menu
{
public int MenuID { get; set; }
public int? ParentID { get; set; }
public string Title { get; set; }
...
public virtual Menu Parent { get; set; }
public virtual ICollection<Menu> Children { get; set; }
}
由于我不想实际删除任何菜单项,因此我将它们在数据库中设置为已删除。
在 linq 查询中,我可以毫无问题地排除标记为已删除的菜单项。
我的问题是,当我将 Children 属性与 Matt Hidiger 的树视图助手结合时,它包含标记为已删除的菜单项: http://www.matthidinger.com/archive/2009/02/08/asp.net-mvc-recursive-treeview-助手.aspx。
ChildrenProperty(root) 返回标记为已删除的项目:
private static void AppendChildren<T>(StringBuilder sb, T root, Func<T, IEnumerable<T>> childrenProperty, Func<T, string> itemContent)
{
var children = childrenProperty(root);
if (children.Equals(null) && !children.Any())
{
sb.AppendLine("</li>");
return;
}
sb.AppendLine("\r\n<ul>");
foreach (T item in children)
{
RenderLi(sb, item, liContent, itemContent);
AppendChildren(sb, item, childrenProperty, liContent, itemContent);
}
sb.AppendLine("</ul></li>");
}
是否可以更改 OnModelCreating 中的映射,以便它包含一个可以排除已删除菜单项的查询?
我的自引用映射:
modelBuilder.Entity<Menu>().HasMany(m => m.Children).WithOptional().HasForeignKey(m => m.ParentID);
我是否走错了路?问题可以用不同的方式解决吗?
I have a POCO Menu class with a self reference children property:
public class Menu
{
public int MenuID { get; set; }
public int? ParentID { get; set; }
public string Title { get; set; }
...
public virtual Menu Parent { get; set; }
public virtual ICollection<Menu> Children { get; set; }
}
As I don't want to actual delete any menu items, I set these in the database as deleted.
In a linq query I have no problem exluding the menu items that is marked as deleted.
My problem is that the Children property includes the menu items marked as deleted, when I'm combinding it with the treeview helper from Matt Hidiger: http://www.matthidinger.com/archive/2009/02/08/asp.net-mvc-recursive-treeview-helper.aspx.
childrenProperty(root) returns the items that is marked as deleted:
private static void AppendChildren<T>(StringBuilder sb, T root, Func<T, IEnumerable<T>> childrenProperty, Func<T, string> itemContent)
{
var children = childrenProperty(root);
if (children.Equals(null) && !children.Any())
{
sb.AppendLine("</li>");
return;
}
sb.AppendLine("\r\n<ul>");
foreach (T item in children)
{
RenderLi(sb, item, liContent, itemContent);
AppendChildren(sb, item, childrenProperty, liContent, itemContent);
}
sb.AppendLine("</ul></li>");
}
Is it posible to alter the mapping in OnModelCreating so it includes a query where I can exclude the deleted menu items?
My self referencing mapping:
modelBuilder.Entity<Menu>().HasMany(m => m.Children).WithOptional().HasForeignKey(m => m.ParentID);
Am I on a wrong track? Can the problem be solved in a different way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
难道你不能在枚举之前过滤列表吗?
couldn't you just filter the list before enumerating over it?
我最终重写了我的 html 助手,这样我就可以过滤掉标记为已删除的项目。
Matt Hidiger 的样本:
我的修复:
当然,我通过 nuget 更新到了实体框架的最新版本。
I ended up rewriting my html helper, so i could filter out the items marked as deleted.
Matt Hidiger's sampel:
My fix:
And of course i updated to latest version of the entity framework through nuget.