Search code examples
silverlightentity-frameworkwcf-ria-servicesdtopresentation-model

How to put entities and collection of entities inside a DTO in Silverlight - WCF RIA?


I would like to make a DTO which contains Entities. How should I do that? Is it possible?

For example I have something like this in my server project:

public class MyCustomDTO
{   
    [Key]
    public int id { get; set; }

    public EntityCollection<MyEntity> list { get; set; }

    public MyEntity2 dummyproperty { get; set; }

    public string name{ get; set; }
}

But on the client side only the basic types are generated, and the collection and the MyEntity2 typed property is not.

My goal is to encapsulate a few different entities into one DTO, instead of collecting them with multiple async queries...

Or what different solutions might be suitable for this scenario? Am I missing something (some attributes) or it's just not supported?


Solution

  • You need to expose the other entities as service methods in addition to your DTO, so that RIA services can track them on the client-side. Your service should look like:

    public class MyDomainService : LinqToEntitiesDomainService<MyContext>
    {
        public IQueryable<MyCustomDto> GetMyCustomDtos()
        {
            //...
        }
    
        public IQueryable<MyEntity> GetMyEntitys()
        {
            //...
        }
    
        public IQueryable<MyEntity2> GetMyEntity2s()
        {
            //...
        }
    }
    

    You'll also need to add the [Include] attribute to your entities so that they are retrieved on the client side.