RIA DomainService not generating client code using

2019-05-26 17:02发布

问题:

I'm working on a Silverlight 4.0 application and am using RIA services. I have created a class on the server-side which has DataContract and DataMember attributes applied to it.

A DomainService exposes this class as a query result and as such, generates code for it on the client. But somehow it doesn't generate code for all properties of the class. Primitive properties of type guid, string, int, bool etc are generated just fine, but if I have a property of my own complex type, that property isn't created on the client.

Here's the class in question:

    [DataContract]
    [KnownType(typeof(SummaryGroup))]
    public class SummaryDataSet
    {
        public SummaryDataSet()
        {

        }

        [KeyAttribute]
        [DataMember]
        public Guid Guid { get; set; }

        [DataMember]
        public SummaryGroup SummaryGroup { get; set; }

    }

The Guid property is created on the client just fine. The SummaryGroup property isn't created on the client. Here's the code for the SummaryGroup:

[DataContract]
public class SummaryGroup
{
    public SummaryGroup()
    {
    }

    [KeyAttribute]
    [DataMember]
    public Guid Guid { get; set; }

    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public int Sequence { get; set; }
}

Both classes are in the same namespace.

Question: why isn't the SummaryGroup property of SummaryDataSet created on the client and what should I do to fix this?

回答1:

WCF RIA cannot handle complex types, but you could try this:

    [DataContract]
    [KnownType(typeof(SummaryDataSet))]
    public class SummaryDataSet
    {
        public SummaryDataSet()
        { }

        [KeyAttribute]
        [DataMember]
        public Guid Guid { get; set; }

        [DataMember]
        [Association("SummarySet_SummaryGrp_FK", "Guid", "Guid")]
        public SummaryGroup SummaryGroup { get; set; }

    }

This gives RIA the information to connect from the SummaryDataSet to the SummaryGroup.

This assumes that you can request both the SummaryDataSet and SummaryGroup from the serverside service.