2012-02-06 147 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