如何在Silverlight中将实体和实体集合放在DTO中 - WCF RIA?

时间:2011-10-29 16:20:58

标签: silverlight entity-framework wcf-ria-services dto presentation-model

我想制作一个包含实体的DTO。我该怎么办?有可能吗?

例如,我的服务器项目中有类似的内容:

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; }
}

但在客户端只生成基本类型,而集合和MyEntity2类型属性则不生成。

我的目标是将一些不同的实体封装到一个DTO中,而不是使用多个异步查询来收集它们......

或者哪种不同的解决方案可能适合这种情况?我错过了某些东西(某些属性)还是只是不支持?

2 个答案:

答案 0 :(得分:0)

您可以在Silverlight客户端和WCF RIA服务之间发送复杂类型,但您的DTO不得将[Key]属性应用于属性。

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; }
}

<强>更新

您需要先安装WCF RIA Services V1.0 SP1 for Silverlight 4,然后才能在应用程序中使用复杂类型。 WCF RIA Services V1.0 SP1是关于此服务包更改的好文章。

答案 1 :(得分:0)

除了DTO之外,您还需要将其他实体公开为服务方法,以便RIA服务可以在客户端跟踪它们。您的服务应如下所示:

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

    public IQueryable<MyEntity> GetMyEntitys()
    {
        //...
    }

    public IQueryable<MyEntity2> GetMyEntity2s()
    {
        //...
    }
}

您还需要将[Include]属性添加到您的实体,以便在客户端检索它们。