在 WCF RIA 服务中使用 DataAnnotations (DisplayColumn)
我创建了一个实体框架 4.0(DB-First)模型,添加了我的部分类并在它们上使用了 DataAnnotations,以便在客户端上拥有完美的 UI。
我的表之间有一些关系,并在我的类之上使用了 DisplayColumn
。例如,我有一个 User
类,该类的顶部具有 [DataColumn("UserName")]
属性。以及一个具有“公共用户发送者”的 Message
类,该类在属性顶部具有 [Ininclude]
属性。
另外,我在 DomainService
中使用了 .Include("User")
来加载与消息相关的用户。
但在我的数据网格中,我看到 User : (UserID)
(UserID=User 实体的 Key 属性),而不是我指定的 UserName
。我查看了 SL 项目中生成的代码,它使用 DisplayColumn
属性正确地装饰了我的 User
类。但是,我仍然在网格中看不到 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
{
}
在我的域服务中:
public IQueryable<Message> GetMessages()
{
return this.ObjectContext.Messages.Include("Owner");
}
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.
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.
Also, I have used .Include("User")
in my DomainService
to load the User who's related to a message.
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.
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.
[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:
public IQueryable<Message> GetMessages()
{
return this.ObjectContext.Messages.Include("Owner");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最后,我不得不使用反射。对于 DataGrid:
对于 Telerik RadGridView:
At last, I had to use Reflection. For DataGrid:
And for Telerik RadGridView: