2011-05-20 192 views
4

PetaPoco已經以實驗性的形式(現在)推出了Multi-POCO查詢。作爲their blog post建議和它提供的代碼看起來不錯,所有在一對一關係,當我們加載多個POCO每行,只要他們不重複的記錄。PetaPoco和多對一,一對多以及多對多關係

當至少有一方是很多關係會發生什麼?其實示例代碼是多對一關係數據。

示例代碼顯然是多對一的關係。我還沒有測試任何PetaPoco代碼,但是博客文章中提供的代碼是做什麼的?是否每個Article都有自己的User對象實例,即使某些對象實例可能是同一個用戶,或者它們是否共享相同的用戶對象實例?

其他許多關係類型?他們是如何工作的?

回答

6

通常我會自己映射這些一對多查詢,如下例所示。

[TableName("Blogs"), PrimaryKey("BlogId")] 
public class Blog { 
    public int BlogId {get;set;} 
    public string Title {get;set;} 

    [Ignore] 
    public IList<Post> Posts {get;set;} 
} 

[TableName("Posts"), PrimaryKey("PostId")] 
public class Post { 
    public int PostId {get;set;} 
    public int BlogId {get;set;} 
    public string Subject {get;set;} 
    public string Content {get;set;} 
} 

public class FlatBlogPost { 
    public int BlogId {get;set;} 
    public string Title {get;set;} 
    public int PostId {get;set;} 
    public string Subject {get;set;} 
    public string Content {get;set;} 
} 

有兩種方法,我可以爲一個博客或沒有太多的工作顯示的帖子列表,所有的博客。

1.Two查詢 -

var Blog = Db.Query<Blog>(1); 
var Posts = Db.Query<Post>("where BlogId = @0", 1); 

2.One查詢=

var flat = Db.Query<FlatBlogPost>("select b.blogid, b.title, p.postid, p.subject, 
      p.content from blogs b inner join posts p on b.blogid = p.blogid where 
      b.blogid = @0", 1); 

var blog = flat 
    .GroupBy(x=> new { x.BlogId, x.Title }) 
    .Select(x=> new Blog { 
     BlogId = x.Key.BlogId, 
     Title = x.Key.Title, 
     Posts = x.Select(y=> new Post{ 
        PostId = y.PostId, 
        BlogId = x.Key.BlogId, 
        Subject = y.Subject, 
        Content = y.Content 
       }).ToList() 
    }); 

然而通常在數2 I將從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-one-to-many-and-many-to-one/

+0

這不是很好。這是一個駭人的解決方法(爲什麼我不喜歡完整的ORM,因爲你最終會破解代碼以使其工作並使其快速運行(呃))。支持多結果集現在不會太難嗎?這絕對不如自動分離列。 ** Dapper支持多結果集**爲什麼PetaPoco不能做同樣的事情。你不需要平面類,你也可以將更少的數據從數據庫傳輸到中間層。 – 2011-05-21 07:26:19

+0

但你的答案仍然值得+1,因爲這是一個可能的解決方案。 – 2011-05-21 07:28:03

0

個人而言,我不認爲你能避免其他數據庫打電話來獲得評論。通過使用IN子句,您可以獲得10篇文章的所有註釋的列表(以文章存儲的相同順序),並循環遍歷它們,隨着您的進行將它們添加到每個article.comments以及comment.articleid更改。我可以看到在單個sql調用中獲取這些信息的唯一方法就是使用聯接,但是每個評論都會得到重複的文章詳細信息,所以也許這不是petapoco的問題,只是其中一種情況'永遠不會是完美的

+0

最新的PetaPoco測試方法顯示了多對一和一對多的可能性https://github.com/toptensoftware/PetaPoco/commit/13ebc74f83a7a83148b65010ba0d834f1e182518 – 2011-06-02 22:13:12

1

我的'一對多'Petapoco配方如下。這些文檔對我來說不夠清楚。在Linqpad中創建一個數據庫連接,它會顯示你可以添加到生成的Petapoco poco類的所有導航屬性。在Linqpad中執行相同的SQL,以確保它獲得您期望的數據。

// subclass the generated Parent table pocos, add navigation prop for children 
[ResultColumn] public List<DecoratedChild> Child { get; set; } 

// subclass the generated Child table pocos, add navigation prop for parent 
[ResultColumn] public DecoratedParent Parent { get; set; }  

// to get children with parent info 
List<DecoratedChild> children = db.Fetch<DecoratedChild, DecoratedParent>(SELECT child.*, parent.* from ...)  

// to get children with parent info, using PetapocoRelationExtensions 
List<Child> children = db.FetchManyToOne<Child, Parent>(child => child.ID, "select child.*, parent.* from ... 

// to get parents with children info, using PetapocoRelationExtensions    
List<Parent> parents = db.FetchOneToMany<Parent, Child>(par => par.ID, child => child.ID != int.MinValue, "select parent.*, child.* from ...  

SQL選擇順序重要,與獲取類型列表相同!導航道具將會有父母或孩子的數據... 與3個級別的電話將是這樣的:

List<DecoratedGrandChild> grandChildColl = db.Fetch<DecoratedGrandChild, DecoratedChild, DecoratedParent>(SELECT grandch.* , child.*, parent.* from ...)