Entity framework 在WCF RIA服务中使用DataAnnotations(DisplayColumn)

Entity framework 在WCF RIA服务中使用DataAnnotations(DisplayColumn),entity-framework,silverlight-4.0,data-annotations,entity-relationship,wcf-ria-services,Entity Framework,Silverlight 4.0,Data Annotations,Entity Relationship,Wcf Ria Services,我已经创建了EntityFramework4.0DBFirst模型,添加了我的分部类,并在它们上使用了DataAnnotation,以便在客户端上有一个完美的UI 我的表和我的类顶部使用的DisplayColumn之间有一些关系。e、 g.我有一个用户类,该类的顶部有[DataColumnUserName]属性。以及一个消息类,该类具有公共用户发送者,该发送者在属性顶部具有[Include]属性 此外,我在DomainService中使用了.IncludeUser来加载与消息相关的用户 但在我的

我已经创建了EntityFramework4.0DBFirst模型,添加了我的分部类,并在它们上使用了DataAnnotation,以便在客户端上有一个完美的UI

我的表和我的类顶部使用的DisplayColumn之间有一些关系。e、 g.我有一个用户类,该类的顶部有[DataColumnUserName]属性。以及一个消息类,该类具有公共用户发送者,该发送者在属性顶部具有[Include]属性

此外,我在DomainService中使用了.IncludeUser来加载与消息相关的用户

但在我的datagrid中,我看到的是User:UserID UserID=用户实体的键属性,而不是我指定的用户名。我在我的SL项目中查看了生成的代码,它正确地用DisplayColumn属性修饰了我的用户类。但是,我仍然无法在我的网格中看到用户名

任何帮助都将不胜感激。 更新:下面是我在代码中的问题:

正如我提到的,在我的自动生成模型中定义了所有者、用户名、MessageId和UserId。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
{
}  
在我的域名服务中:

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

最后,我不得不使用反射。对于DataGrid:

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;
 }

发布你的代码,否则!哈哈,但说真的,如果你发布代码而不是仅仅谈论代码,这会更容易帮助你。@Robotsushi:在被杀之前更新了我的问题:p