PetaPoco 收藏属性

发布于 2024-12-08 02:19:52 字数 841 浏览 4 评论 0 原文

PetaPoco 中水合作为集合的 POCO 属性的正确方法是什么?我想做这样的事情,但不知道如何做。

return db.Fetch<ColorCategory, List<SubColor>, ColorCategory>((c, s) => { c.SubColors = *?*; return c; }, "SELECT [ColorCategory].[ColorCategoryID] " +
            ",[ColorCategory].[DisplayOrder] " +
            ",[ColorCategory].[Name] " +
            ",[ColorCategory].[ModifiedDate] " +
            ",[ColorCategory].[ModifiedUser] " +
            ",[SubColor].[SubColorID] " +
            ",[SubColor].[Name] " +
            "FROM [ColorCategory] " +
            "INNER JOIN [ColorCategorySubColor] ON [ColorCategory].[ColorCategoryID] = [ColorCategorySubColor].[ColorCategoryID] " +
            "INNER JOIN [SubColor] ON [ColorCategorySubColor].[SubColorID] = [SubColor].[SubColorID]");

其中 ColorCategory 类有一个 List 属性 SubColors。

What is the proper way in PetaPoco to hydrate a POCO property that is a collection? I'd like to do something like this, but not sure how.

return db.Fetch<ColorCategory, List<SubColor>, ColorCategory>((c, s) => { c.SubColors = *?*; return c; }, "SELECT [ColorCategory].[ColorCategoryID] " +
            ",[ColorCategory].[DisplayOrder] " +
            ",[ColorCategory].[Name] " +
            ",[ColorCategory].[ModifiedDate] " +
            ",[ColorCategory].[ModifiedUser] " +
            ",[SubColor].[SubColorID] " +
            ",[SubColor].[Name] " +
            "FROM [ColorCategory] " +
            "INNER JOIN [ColorCategorySubColor] ON [ColorCategory].[ColorCategoryID] = [ColorCategorySubColor].[ColorCategoryID] " +
            "INNER JOIN [SubColor] ON [ColorCategorySubColor].[SubColorID] = [SubColor].[SubColorID]");

Where the ColorCategory class has a List property, SubColors.

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

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

发布评论

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

评论(1

旧伤还要旧人安 2024-12-15 02:19:52

您可以在 sql 查询中加入 SubColors,这样您就可以获得每个 SubColor 的重复 ColorCategory 记录,如下所述:

http://www.toptensoftware.com/Articles/115/PetaPoco-Mapping-One-to-Many-and-Many-to-One-Relationships

官方方法(目前)是创建一个关系器类,其中包含一些状态(关于当前记录)和 PetaPoco 将为结果集中的每一行调用的方法。相关器告诉 PetaPoco 何时停止向 ColorCategory 添加子颜色并移至下一个父 poco。

然而,Schotime 添加了一些出色的功能可以自动执行此操作

这在我的 GitHub 上的测试应用中进行了演示。编程非常简单,但请注意 SELECT 列的顺序至关重要,因为 PetaPoco 依赖于与 Fetch 中的流程相同的流程。波科列表。

另一种方法是 N+1 方法,您可以根据需要加载子颜色。

You can join the SubColors in the sql query, so you get back a duplicate ColorCategory record for every SubColor as explained here :

http://www.toptensoftware.com/Articles/115/PetaPoco-Mapping-One-to-Many-and-Many-to-One-Relationships

The offcial method (at the moment) is to create a relator class containing some state (about the current record) and a method which PetaPoco will invoke for every row in the resultset. The relator tells PetaPoco when to stop adding SubColors to the ColorCategory and move onto the next parent poco.

However, Schotime has added some brilliant functions that automate this.

This is demonstrated in my test app at GitHub. It's quite simple to program but be aware that the order of the SELECT columns is crucial as PetaPoco relies on the flow being the same as in the Fetch<T1, T2...> poco list.

The alternative is an N+1 approach where you load the SubColors on demand.

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