POCO 自引用多对多

发布于 2024-11-02 15:47:39 字数 562 浏览 0 评论 0原文

这看起来像是同一个问题,但我的问题有点不同。

实体框架 4 CTP 5 自引用多对多 示例代码

public class Category 
{
    [Key]
    public int Id { get; set; }

    public string Name { get; set; }

    public virtual ICollection<Category> Parents { get; set; }
    public virtual ICollection<Category> Children { get; set; }        
}

当我将字段“父级”定义为“类别”而不是“父级”作为“列表”时,我得到了正确的结果。

Category类设计得很好,但是POCO呢?我应该怎么办?提前致谢。

It is look like same question but mine little bit different.

Entity Framework 4 CTP 5 Self Referencing Many-to-Many

sample code;

public class Category 
{
    [Key]
    public int Id { get; set; }

    public string Name { get; set; }

    public virtual ICollection<Category> Parents { get; set; }
    public virtual ICollection<Category> Children { get; set; }        
}

I got right result when I define a field Parent as Category, instead of parents as List.

Category class well designed, but POCO? What should I do? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

著墨染雨君画夕 2024-11-09 15:47:39

你的课程对我来说效果很好,无需任何定制。甚至 [Key] 属性也不是必需的。

下面是一些练习这个模型的代码:

using (var context = new MyContext())
{
    var parent1 = new Category { Name = "Parent 1" };
    var parent2 = new Category { Name = "Parent 2" };
    var child1 = new Category { Name = "Child 1" };
    var child2 = new Category { Name = "Child 2" };
    parent1.Children = new List<Category> { child1, child2 };
    parent2.Children = new List<Category> { child1, child2 };
    context.Categories.Add(parent1);
    context.Categories.Add(parent2);
    context.SaveChanges();
}
using (var context = new MyContext())
{
    var categories = context.Categories.OrderByDescending(x => x.Children.Count)
                                       .ToList();
    foreach (var category in categories)
    {
        Console.Write(category.Name + ": ");
        Console.WriteLine("Parents ({0}) Children ({1})",
            string.Join(",", category.Parents.Select(x => x.Name).ToArray()),
            string.Join(",", category.Children.Select(x => x.Name).ToArray()));
    }
}

这将打印:

Parent 1: Parents () Children (Child 1,Child 2)
Parent 2: Parents () Children (Child 1,Child 2)
Child 1: Parents (Parent 1,Parent 2) Children ()
Child 2: Parents (Parent 1,Parent 2) Children ()

Your class works fine for me without any customizations. Even the [Key] attribute is not required.

Here's some code that exercises this model:

using (var context = new MyContext())
{
    var parent1 = new Category { Name = "Parent 1" };
    var parent2 = new Category { Name = "Parent 2" };
    var child1 = new Category { Name = "Child 1" };
    var child2 = new Category { Name = "Child 2" };
    parent1.Children = new List<Category> { child1, child2 };
    parent2.Children = new List<Category> { child1, child2 };
    context.Categories.Add(parent1);
    context.Categories.Add(parent2);
    context.SaveChanges();
}
using (var context = new MyContext())
{
    var categories = context.Categories.OrderByDescending(x => x.Children.Count)
                                       .ToList();
    foreach (var category in categories)
    {
        Console.Write(category.Name + ": ");
        Console.WriteLine("Parents ({0}) Children ({1})",
            string.Join(",", category.Parents.Select(x => x.Name).ToArray()),
            string.Join(",", category.Children.Select(x => x.Name).ToArray()));
    }
}

This will print:

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