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

发布于 2024-11-15 03:28:58 字数 1356 浏览 3 评论 0原文

我创建了一个实体框架 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不是你的备胎 2024-11-22 03:28:58

最后,我不得不使用反射。对于 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;
 }

At last, I had to use Reflection. For 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:

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;
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文