Silverlight 4.0 未加载用于服务器端验证的实体上的关联属性

Silverlight 4.0 未加载用于服务器端验证的实体上的关联属性,silverlight-4.0,wcf-ria-services,Silverlight 4.0,Wcf Ria Services,考虑以下情况。我有一个名为ProductSupplier的实体,它是一个表示模型。它是通过对产品和供应商进行内部连接,并从Linq语句中创建新的投影来创建的。ProductSupplier投影还创建PartType对象列表,这也是一个表示模型 public partial class ProductSupplier { private IEnumerable<PartType> _partTypes; [Key] public int ProductSupp

考虑以下情况。我有一个名为ProductSupplier的实体,它是一个表示模型。它是通过对产品和供应商进行内部连接,并从Linq语句中创建新的投影来创建的。ProductSupplier投影还创建PartType对象列表,这也是一个表示模型

public partial class ProductSupplier
{
    private IEnumerable<PartType> _partTypes;

    [Key]
    public int ProductSupplierKey { get; set }

    [Include]
    [Association("ProductSupplier_PartType", "ProductSupplierKey", "ProductSupplierKey")]
    public IEnumerable<PartType> PartTypes
    {
        get { return _partTypes ?? (_partTypes = new List<PartType>()); } 
        set { if (value != null) _partTypes = value; }
    }
}

public partial class PartType
{
    [Key]
    public int PartTypeKey { get; set; }

    [Key]
    public int ProductSupplierKey { get; set; }

    public int PartQuantity { get; set; }
}
当从客户端调用验证时,这可以正常工作。我遇到的问题是,当验证程序从服务器端运行时,IEnumerable总是空的

我曾尝试将[RoundTripOriginal]添加到PartQuantity和其他各种属性,例如所有键字段,但在服务器端完成时,它仍然是一个空列表


在服务器上运行验证时,如何访问这些PartType对象?

不幸的是,您无法保证对象图在服务器上运行时的状态。RIA优化了东西,所以你只能看到修改过的实体。一种解决方案是使用复合材料。它将确保传递整个图形,但它具有其他可能不是您想要的效果。另一种选择是在更新方法中添加图表,然后执行验证,并根据需要抛出ValidationException。

我已经接受了这个答案。我只想让搜索这个答案的人明白:用[Composition]而不是[Association]赋予属性将强制将整个对象图传递回服务。
public static ValidationResult ValidatePartTotals(ProductSupplier productSupplier, ValidationContext validationContext)
{
    if (productSupplier.PartTypes.Sum(p => p.PartQuantity) > 10)
        return new ValidationResult("Must be less than 10 parts total.");

    return ValidationResult.Success;
}