带查询的 EF CTP5 自引用模型

发布于 2024-10-21 22:15:52 字数 1564 浏览 3 评论 0原文

我有一个带有自引用子属性的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ヤ经典坏疍 2024-10-28 22:15:52

难道你不能在枚举之前过滤列表吗?

foreach (T item in children.Where(x => x.IsDeleted == false)
{
    RenderLi(sb, item, liContent, itemContent);
    AppendChildren(sb, item, childrenProperty, liContent, itemContent);
}

couldn't you just filter the list before enumerating over it?

foreach (T item in children.Where(x => x.IsDeleted == false)
{
    RenderLi(sb, item, liContent, itemContent);
    AppendChildren(sb, item, childrenProperty, liContent, itemContent);
}
很酷不放纵 2024-10-28 22:15:52

我最终重写了我的 html 助手,这样我就可以过滤掉标记为已删除的项目。

Matt Hidiger 的样本:

<%= Html.TreeView("locations", Model.Locations, l => l.ChildrenLocations, l => l.Name) %>

我的修复:

<%= Html.TreeView("locations", Model.Locations, l => l.ChildrenLocations.Where(x => x.Deleted == false), l => l.Name) %>

当然,我通过 nuget 更新到了实体框架的最新版本。

I ended up rewriting my html helper, so i could filter out the items marked as deleted.

Matt Hidiger's sampel:

<%= Html.TreeView("locations", Model.Locations, l => l.ChildrenLocations, l => l.Name) %>

My fix:

<%= Html.TreeView("locations", Model.Locations, l => l.ChildrenLocations.Where(x => x.Deleted == false), l => l.Name) %>

And of course i updated to latest version of the entity framework through nuget.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文