2012-02-06 83 views
1

我正在使用多對多數據庫和通過外鍵連接主數據表的連接表。替代Group_Concat來定製現有的實體框架模型

http://img12.imageshack.us/img12/6500/databasew.png

用我的數據表示,它需要從屬表的字段(例如Genres.name)的若干行連接成數據綁定網格的一個小區。

書名|流派

有些書|小說;恐怖;神祕


隨着我用這個GROUP_CONCAT SQL查詢用於此目的的數據集:

SELECT Books.ID, Books.BOOKTITLE, GROUP_CONCAT(Genres.name, '; ') As Gen 

FROM Books INNER JOIN 

     Books_Genres ON Books.ID = Books_Genres.book_id INNER JOIN 
     Genres ON Books_Genres.genre_id =Genres.id 

GROUP BY Books.ID 

就像現在我上移動到EF4並且是很新的,我不知道如何實現可以在包含實體的數據網格中顯示的相同結果表。應該對實體模型做出什麼樣的改變才能得到相同的結果?

任何幫助非常感謝!


更新:

我有數據庫(Model1.edmx & Model1.Designer.cs)產生的實體模型。我是否需要編輯Model1.edmx(xml文件)?一步一步的指示將大大幫助。我剛剛開始EF4,這對我來說都是希臘語。 :)

我做了一個自定義類:

public partial class Book { 
     [EdmScalarProperty(EntityKeyProperty = false, IsNullable = true)] 
     [DataMember()] 
     public global::System.String GenresConcatenated 
     { 
      get { return string.Join("; ", Genres.Select(g => g.name));} 
      set {} 
     } 
     private List<Genre> Genres{ get; set; } 
    } 

現在我可以訪問IDE的智能GenresConcatenated屬性,但是當我運行應用程序,它拋出的錯誤:

The number of members in the conceptual type 'MyNamespace.Book' does not match with the number of members on the object side type 'MyNamespace.Common.Book'. Make sure the number of members are the same.

它看起來像:

... EntityDataSource does not support custom properties in the partial classes. It only returns the entity that is in the model. (Binding custom property in Entity Framework)

所以我又回到原點了。 :)

+1

不要進行任何更新自動生成的。由EF生成的所有類都是部分的。創建您自己的Book類的部分部分,並添加自定義屬性連接類型,如@Bas所示。 – 2012-02-07 07:41:18

+0

感謝您的提示,拉迪斯拉夫會盡力做到這一點。這個新屬性可以在我嘗試綁定到數據網格的數據源表中使用嗎? – Planescaped 2012-02-07 18:19:22

回答

0

怎麼樣,

class Book 
{ 
    public int ID { get; set; } 
    public string Title { get; set; } 
    public List<Genre> Genres { get; set; } 

    public string Gen 
    { 
     get 
     { 
      return string.Join("; ", Genres.Select(g => g.Name)); 
     } 
    } 
} 

然後綁定到Book.Gen(或任何你想將它命名)

+0

我做了財產,我如何將它綁定到Book.Gen? – Planescaped 2012-02-08 00:03:45