1
votes

Can I add a serializable property to an Entity or ComplexType using RIA Services? Or can I create a DTO that contains an EntityObject using RIA? If it's not possible, what's the workaround?

I've seen How Can I Extend A Entity When Using RIA Services With Silverlight? which deals with adding a property to a Entity, but it's a data-less property that merely wraps existing properties. In other words it's not holding any new data. I apologise for the length of this question.

I have two results (one EntityObject from a table query, and one ComplexObject from a stored proc) in my data layer that I want to return together & in a single service call. They are inextricably linked from a presentation point of view (one doesn't make sense without the other) and it would be unseemly to do two calls and then join them up client side. But I'm having problems getting the two objects in a single result.


First I tried to extend the ComplexObject by adding a property for the Entity

public partial class MyProcResult
{        
    [DataMember]
    public MyEntity Foo { get; set; }
}

and populating it in the domain service operation. The property is set on server side but it doesn't get serialized - in fact the client doesn't even see the property!

After some reading it's apparent that I can make the client aware of properties by naming my partial class MyProcResult.shared.cs - and now the client knows the property exists, but it's still not serialized.

So I assume that the RIA service isn't exposing a very thorough service contract and try something else. My next attempt is to create a DTO in which I'll include my two objects - should work, right?

[DataContract]
public class MyContainer
{
    [DataMember]
    public MyProcResult Bar { get; set; }
    [DataMember]
    public MyEntity Foo { get; set; }
}

Again, it's all populated before serialization. This time the client can't see the MyEntity property. It thinks that MyContainer only has the MyProcResult property. The generated code on the client side does not contain the other property!

What's going on? What is special about these Entities that stops them from being serialized when marked [DataMember], or when included in a DTO?

2
Just some random thought: Does MyEntity inherit from Entity? (i see MyContainer doesn't). More importantly, Does MyProcResult inherit from ComplexObject? Have you tried the include attribute? stating from msn: "Complex type members are always serialized deeply based on DataContract/DataMember annotations. This contrasts with entity associations, whose serialization is governed by the application of IncludeAttribute."Polity
@Polity - Yes, MyEntity inherits from EntityObject and appears on the EDMX designer, and MyProcResult inherits from ComplexObject and appears as a complex type in the Model Browser pane. You can see in my second sample I'm just marking these as DataMember, not complex type or entity associations. I would expect they be serialized as regular .NET objects...Kirk Broadhurst

2 Answers

2
votes

For complex eneities, you have to give relationship for them(foreign key for instance), otherwise it's not possible to expose composite eneity to client. For example:

this sample wraps category entity collection in previous Product entity:

public partial class ProductWrapper 
{
     [Key]     
     public Guid Id { get; set; }     
     public string Name { get; set; }

     [Include]
     [Association("FK_ProductsWrapper", "Id", "CategoryId")]     
     public Category[] CategoryList { get; set; } 
}
1
votes

When property is of type EntityObject, only if it shares an association and it is a navigation property, it will be exposed to client. Since entities can only be retrieved via query and change tracking must work correctly any property deriving from entity object must share an association.