1

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 Answers2

0

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.

Ekk
  • 5,627
  • 19
  • 27
  • I tried but then I get an error saying that "The entity in DomainService does not have a key defined. Entities exposed by DomainService operations must have at least one public property marked with the KeyAttribute" So like I've read here http://forums.silverlight.net/t/202531.aspx , RIA Domain Service can only work with entities, so you can't just send a complex type, only as part of an entity. – seekingtheoptimal Oct 30 '11 at 13:07
  • You need to install WCF RIA SP1 for Silverlight, I just update my answer. – Ekk Oct 30 '11 at 13:14
  • I did but still not working. I think because my DomainService is a LinqToEntitiesDomainService I just cant send back anything else then entities. Should I make another DomainService which is not inherited from LinqToEntitiesDomainService? (my current domain service was generated from an .edmx file and I extended it with custom queries in a separate file after I made the service class partial) – seekingtheoptimal Oct 30 '11 at 16:31
0

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.

Tevin
  • 1,394
  • 16
  • 26