服务中的自定义属性在客户端上不可用

服务中的自定义属性在客户端上不可用

本文介绍了RIA 服务中的自定义属性在客户端上不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 StatusUpdates 的实体.没有自我加入.但是,我想包含一个类型为 StatusUpdates 的 InternalStatusUpdates 列表(我再次提到数据库中没有自联接).所以我在同一个命名空间中创建了一个部分类,并创建了一个名为 InternalStatusUpdates 的属性,并在其上包含了 [DataMember] 属性.但它仍然没有出现在客户端.这是我的部分课程,具体如下:-

I have an entity called StatusUpdates. There is no self join in it. However i want to include a list of InternalStatusUpdates which is of type StatusUpdates (Again i would mention there is no self join in database). So i created a partial class in the same namespace and created a property named InternalStatusUpdates and included [DataMember] attribute on it. But it still doesn't appear on the client. This is my partial class to be specific :-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects.DataClasses;
using System.Runtime.Serialization;
using System.ServiceModel.DomainServices.Server;
using System.ComponentModel.DataAnnotations;

namespace FMT.Data.Web
{
    public partial class StatusUpdate : EntityObject
    {

         private List<StatusUpdate> _internalListStatusUpdates = new List<StatusUpdate>();
         [DataMember]
         public List<StatusUpdate> InternalListStatusUpdates
         {
             get { return _internalListStatusUpdates; }
             set {
                 _internalListStatusUpdates = value;
                 OnPropertyChanged("InternalListStatusUpdates");
             }
         }
    }
}

有趣的是,如果我将 List 更改为 List,它在客户端上可用!我该如何解决这个问题?

Interesting thing is if i change List<StatusUpdate> to List<string> it is available on the client! How do i solve this problem?

我认为因为它是实体类型,所以我必须指定 [Include].当我这样做时,我收到以下错误:-

I thought because it is entity type i will have to specify [Include]. When i did that i get the following error :-

Invalid Include specification for member 'StatusUpdate.InternalListStatusUpdates'. Non-projection includes can only be specified on members with the AssociationAttribute applied.

然后我继续指定关联,所以现在我的属性看起来像:-

Then i went ahead and specified association, so now my property looks like :-

private List<StatusUpdate> _internalListStatusUpdates = new List<StatusUpdate>();
     [DataMember]
    [Include]
    [Association("internalUpdates", "StatusUpdatesId", "StatusUpdatesId")]
     public List<StatusUpdate> InternalListStatusUpdates
     {
         get { return _internalListStatusUpdates; }
         set {
             _internalListStatusUpdates = value;
             OnPropertyChanged("InternalListStatusUpdates");
         }
     }

其中 StatusUpdatesId 是我在 StatusUpdates 实体中的主键,而 internalUpdates 只是一个任意选择的名称.现在构建成功并且该属性在客户端上可用,但我从服务器填充的数据在客户端上不可用.我该如何解决?

where StatusUpdatesId is my primary key in StatusUpdates Entity and internalUpdates is just a arbitary name chosen. Now the build is successfull and the property is available on the client but the data that i populate from server is not available on the client. How do i solve it?

提前致谢:)

推荐答案

AssociationAttribute 中的第三个参数是列表元素中外键属性的名称.请务必记住,RIA 服务将实体放入其相应的EntitySet 中,然后对主键和外键应用过滤器,而不是使用对其他对象的真实引用.

The third parameter in AssociationAttribute is the name of the foreign-key property in the list elements. It is important to keep in mind that RIA Services puts entities in their corresponding EntitySet<T> and then applies filters on primary key and foreign key instead of using true references to the other objects.

在给定的情况下,您可能必须为您的列表元素定义一个属性ParentUpdatesId",并用您的父 StatusUpdate 的主键填充它.

In the given case you will probably have to define a property "ParentUpdatesId" for your list elements and fill that with the primary key of your parent StatusUpdate.

示例:

StatusUpdate su = new StatusUpdate { StatusUpdatesId = 123 };
StatusUpdate child1 = new StatusUpdate { StatusUpdatesId = 1001, ParentUpdatesId = 123 };
StatusUpdate child2 = new StatusUpdate { StatusUpdatesId = 1002, ParentUpdatesId = 123 };
su.InternalListStatusUpdates.Add(child1);
su.InternalListStatusUpdates.Add(child2);

您的协会将如下所示:

[Include]
[Association("internalUpdates", "StatusUpdatesId", "ParentUpdatesId")]
public List<StatusUpdate> InternalListStatusUpdates { ... }

这篇关于RIA 服务中的自定义属性在客户端上不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 06:17