I have created an entity framework 4.0 (DB-First) model, added my partial classes and used DataAnnotations
on them to have a perfect UI on the client.
我创建了一个实体框架4.0(DB-First)模型,添加了我的部分类,并在它们上使用DataAnnotations在客户端上拥有一个完美的UI。
I have some relations between my tables and used DisplayColumn
on top my classes. e.g. I have a User
class that has [DataColumn("UserName")]
attribute on top of the class. And a Message
class which has "public User Sender" which has [Include]
attribute on top of the property.
我的表之间有一些关系,并在我的类上使用DisplayColumn。例如我有一个User类,它在类的顶部有[DataColumn(“UserName”)]属性。还有一个Message类,它有“public User Sender”,它在属性顶部有[Include]属性。
Also, I have used .Include("User")
in my DomainService
to load the User who's related to a message.
另外,我在我的DomainService中使用.Include(“User”)来加载与消息相关的用户。
But in my datagrid, I see User : (UserID)
(UserID=Key property of User entity) instead of UserName
that I have specified. I looked in the generated code in my SL project and it correctly decorated my User
class with DisplayColumn
attribute. But still, I cannot see UserName
in my grid.
但在我的datagrid中,我看到User:(UserID)(UserID = User实体的Key属性)而不是我指定的UserName。我在SL项目中查看了生成的代码,并使用DisplayColumn属性正确地修饰了我的User类。但是,我仍然无法在我的网格中看到UserName。
Any help would be greatly appreciated.
Update: Here's my question in code:
任何帮助将不胜感激。更新:这是我在代码中的问题:
As I have mentioned, Owner
, UserName
, MessageId
, UserId
have been defined in my auto-generated model. UserMeta
class has nothing special.
正如我所提到的,在我自动生成的模型中定义了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
{
}
In my DomainService:
在我的DomainService中:
public IQueryable<Message> GetMessages()
{
return this.ObjectContext.Messages.Include("Owner");
}
1 个解决方案
#1
0
At last, I had to use Reflection. For DataGrid:
最后,我不得不使用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;
}
}
And for Telerik RadGridView:
对于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;
}
#1
0
At last, I had to use Reflection. For DataGrid:
最后,我不得不使用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;
}
}
And for Telerik RadGridView:
对于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;
}