问题描述
|
我创建了一个实体框架4.0(DB-First)模型,添加了我的部分类,并在它们上使用了“ 0”以在客户端上具有完美的UI。
我的表之间有一些关系,并且在课堂上使用
DisplayColumn
。例如我有一个User
类,该类的顶部具有[DataColumn(\"UserName\")]
属性。还有一个具有\“ public User Sender \”的Message
类,该属性在该属性的顶部具有[Include]
属性。
另外,我在DomainService
中使用了.Include(\"User\")
来加载与邮件相关的用户。
但是在我的数据网格中,我看到的是“ 8”(UserID =用户实体的键属性),而不是我指定的“ 9”。我在SL项目中查看了生成的代码,它正确地用DisplayColumn
属性装饰了User
类。但是,仍然看不到网格中的“ 9”。
任何帮助将不胜感激。
更新:这是我在代码中的问题:
如前所述,在我自动生成的模型中已定义了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\");
}
解决方法
最后,我不得不使用反射。对于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;
}