PetaPoco 和多对一、一对多和多对多关系
PetaPoco 已以实验形式引入了 Multi-POCO 查询(目前)。正如他们的博客文章建议并且它提供的代码看起来不错,并且当我们每行加载多个 POCO 时,只要它们不重复记录,所有这些都处于一对一关系中。
当至少一侧是多关系时会发生什么?实际上示例代码是多对一关系数据。
示例代码显然是多对一关系。我还没有测试任何 PetaPoco 代码,但是博客文章中提供的代码有什么作用?是否每个 Article
都有自己的 User
对象实例,即使有些可能是同一用户,或者它们共享相同的用户对象实例?
那么其他Many关系类型呢?它们到底是如何工作的?
PetaPoco has introduced Multi-POCO queries in experimental form (for now). As their blog post suggests and the code it provides this looks nice and all in One-to-One relations when we load multi POCOs per row as long as they don't repeat over the records.
What happens when at least one side is many relation? Actually example code is Many-to-One relational data.
Example code is clearly a Many-to-One relation. I haven't tested any PetaPoco code but what does the provided code on the blog post do? Does every Article
have their own User
object instance even though some may be the same user or do they share the same user object instance?
And what about other Many relation types? How do they work of they work at all?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通常我自己映射这些一对多查询,如下例所示。
我可以通过两种方式显示一个博客的帖子列表,或者无需太多工作即可显示所有博客的帖子列表。
1.两个查询 -
2.一个查询 =
但是通常在数字 2 中,我会直接从 FlatBlogPost 对象映射到我需要显示数据的视图模型。
更新
查看这些帮助程序,它们扩展了 PetaPoco 以支持基本的一对多和多对一查询。
schotime.net/blog/index.php/2011/08/21/petapoco-one-to-many-and-many-to-one/https://schotime.wordpress.com/2011/08/21/petapoco -一对多和多对一/Usually I map these one-to-many queries myself like the following example.
There are two ways I could display a list of posts for one blog or without too much work, all blogs.
1.Two queries -
2.One query =
However usually in number 2 I would map directly from the FlatBlogPost object to my viewmodel for which I need to display the data.
Update
Check out these helpers which extend PetaPoco to support basic One-to-Many and Many-to-One queries.
schotime.net/blog/index.php/2011/08/21/petapoco-one-to-many-and-many-to-one/https://schotime.wordpress.com/2011/08/21/petapoco-one-to-many-and-many-to-one/我的 Petapoco 的“一对多”食谱如下。这些文档对我来说还不够清楚。在 Linqpad 中创建一个数据库连接,它将显示您可以添加到生成的 Petapoco poco 类中的所有导航属性。在 Linqpad 中执行相同的 SQL,以确保它获得您期望的数据。
SQL 选择顺序很重要,与获取类型列表中的相同!
导航道具将有父级或子级数据......
具有 3 个级别的调用将类似于:
My 'One to Many' recipe for Petapoco is below. The docs are not clear enough for me. Create a db connection in Linqpad, it will show you all Navigation properties you can add to generated Petapoco poco classes. Execute the same SQL in Linqpad, to make sure it gets the data you expect.
SQL select order important, same as in Fetch types list !!!
navigation props will have parent or children data ...
with 3 levels the call will be like:
就我个人而言,我认为您无法避免再次调用数据库来获取评论。您可以使用 IN 子句获取 10 篇文章的所有评论列表(与文章存储的顺序相同),然后循环遍历它们,将它们添加到每篇文章的评论中,并且 comment.articleid 会发生变化。我可以看到在单个 sql 调用中获取此信息的唯一方法是使用联接,但随后您会获得每个评论的重复文章详细信息,所以也许这不是 petapoco 的问题,只是其中之一永远不会完美
Personally I don't think you can avoid another database call to get the comments. You could get a list of all comments for the 10 articles (in the same order the articles are stored) by using an IN clause, and loop through them adding them to each article.comments as you go along and the comment.articleid changes. The only way I can see getting this information in a single sql call would be to use a join but then you'd get duplicate article details for each comment, so maybe this isn't a problem with petapoco, just one of those things that'll never be perfect