0

我已經創建了一個實體框架4.0(DB-First)模型,添加了我的部分類並在其上使用了DataAnnotations以在客戶端上擁有完美的用戶界面。在WCF RIA Services中使用DataAnnotations(DisplayColumn)

我在我的表格之間有一些關係,並且在我的課程上面使用了DisplayColumn。例如我有一個User類,在類的頂部有[DataColumn("UserName")]屬性。和一個Message類有「公共用戶發件人」,其中屬性頂部[Include]屬性。

此外,我在我的DomainService中使用了.Include("User")來加載與消息相關的用戶。

但在我的DataGrid中,我看到,我已指定User : (UserID)(用戶ID =用戶實體的關鍵屬性),而不是UserName。我在我的SL項目中查看了生成的代碼,並且使用DisplayColumn屬性正確地修飾了User類。但是,我仍然無法在我的網格中看到UserName

任何幫助將不勝感激。
更新:這裏是我的代碼的問題:

正如我剛纔所說,OwnerUserNameMessageIdUserId已經在我的自動生成的模型定義。 UserMeta類沒有什麼特別的。

[MetadataType(typeof(MessageMeta))] 
public partial class Message 
{ 
} 

public class MessageMeta 
{ 
[Include()] 
[Display(Name = "Belongs to", Order = 4)] 
[Association("Message_User","MessageId","UserId",IsForeignKey = true)] 
public virtual User Owner { get; set; } 
} 

[MetadataType(typeof(UserMeta))] 
[DisplayColumn("UserName")] 
public partial class User 
{ 
} 

在我的DomainService:

public IQueryable<Message> GetMessages() 
{ 
    return this.ObjectContext.Messages.Include("Owner"); 
} 
+0

發佈您的代碼或別的!大聲笑,但認真這將是很容易的幫助,如果你發佈的代碼,而不是隻是談論它。 – BentOnCoding 2011-06-14 14:11:18

+0

@Robotsushi:在殺死之前更新了我的問題:p – Kamyar 2011-06-14 14:28:15

回答

0

最後,我不得不使用反思。對於數據網格:

private void OnAutoGenerateColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) 
    { 
     //Need to get an array, but should always just have a single DisplayColumnAttribute 
     var atts = e.PropertyType.GetCustomAttributes(typeof(DisplayColumnAttribute),true); 

     foreach (DisplayColumnAttribute d in atts) 
     { 
      DataGridTextColumn col = (DataGridTextColumn)e.Column; 

      //Make sure that we always have the base path 
      if(col.Binding.Path.Path!="") 
      { 
       col.Binding = new Binding() 
       { 
        Path = new PropertyPath(col.Binding.Path.Path + "." + d.DisplayColumn) 
       }; 
      } 

      //Only do the first one, just in case we have more than one in metadata 
      break; 
     } 

    } 

而對於Telerik的RadGridView:

var column = e.Column as GridViewDataColumn; 
if (column == null) 
{ 
    return; 
} 

// Need to get an array, but should always just have a single DisplayColumnAttribute 
var atts = column.DataType.GetCustomAttributes(typeof(DisplayColumnAttribute), true); 

foreach (DisplayColumnAttribute d in atts) 
{ 
    // Make sure that we always have the base path 
    if (column.DataMemberBinding.Path.Path != "") 
    { 
     column.DataMemberBinding = new Binding() 
     { 
      Path = new PropertyPath(column.DataMemberBinding.Path.Path + "." + d.DisplayColumn) 
     }; 
    } 
    // Only do the first one, just in case we have more than one in metadata 
    break; 
}