我创建了一个实体框架4.0(DB-First)模型,添加了我的部分类并在其上使用DataAnnotations
在客户端上拥有完美的UI。
我的表之间有一些关系,并在我的课程中使用DisplayColumn
。例如我有一个User
类,在类的顶部有[DataColumn("UserName")]
属性。还有一个Message
类,其中包含“公共用户发件人”,该属性具有[Include]
属性。
此外,我在.Include("User")
中使用了DomainService
来加载与邮件相关的用户。
但在我的数据网格中,我看到User : (UserID)
(UserID = User实体的Key属性)而不是我指定的UserName
。我查看了SL项目中生成的代码,并使用User
属性正确装饰了DisplayColumn
类。但是,我在网格中看不到UserName
。
非常感谢任何帮助 更新:以下是我在代码中提出的问题:
正如我所提到的,Owner
,UserName
,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
{
}
在我的DomainService中:
public IQueryable<Message> GetMessages()
{
return this.ObjectContext.Messages.Include("Owner");
}
答案 0 :(得分:0)
最后,我不得不使用Reflection。对于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;
}