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

1.8k Views Asked by At

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?

2

There are 2 best solutions below

0
On BEST ANSWER

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.

3
On

You can send complex type between Silverlight client and WCF RIA service but your DTO must not have [Key] attribute apply to property.

public class MyCustomDTO
{   
    //[Key] // comment this line and there you go.
    public int id { get; set; }

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

    public MyEntity2 dummyproperty { get; set; }

    public string name{ get; set; }
}

Update

You need to install WCF RIA Services V1.0 SP1 for Silverlight 4 before you can use complex type in your application. WCF RIA Services V1.0 SP1 is good article about change in this service pack.