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?
EntityObject
and appears on the EDMX designer, and MyProcResult inherits fromComplexObject
and appears as a complex type in the Model Browser pane. You can see in my second sample I'm just marking these asDataMember
, not complex type or entity associations. I would expect they be serialized as regular .NET objects... – Kirk Broadhurst