【问题标题】:exposing Associated entities in ria services暴露 ria 服务中的关联实体
【发布时间】:2011-01-31 16:55:52
【问题描述】:

我已阅读此链接:-

RIA Services: Entity Framework Reference Entities

并在元数据类以及我的域服务查询中添加了[Include]。

在我的客户端,我的Albums 实体中有一个名为Photos 的导航属性。但是它是 EntityCollection 类型的,我不知道如何迭代它。它没有 get 方法,甚至索引也不适用于它。我尝试了以下方法:-

albums.photos[0]

但它不起作用。谁能告诉我如何在我的相册实体中迭代该照片集?

提前致谢:)

【问题讨论】:

    标签: .net wcf-ria-services


    【解决方案1】:

    因此,在您的相册类的元数据中,您有如下内容:

    [MetadataTypeAttribute( typeof(Album.AlbumMetadata ) )]
    public partial class Album
    {
      internal sealed class AlbumMetadata
      {
        private AlbumMetadata ()
        { }
    
        [Include]
        public EntityCollection<Photo> Photos { get; set; } 
    }
    

    在您的域服务中,您将拥有如下内容:

        public IEnumerable<Album> GetAlbums ()
        {
            var albums = from a in ObjectContext.Albums.Include( "Photos" )
                         orderby a.AlbumId descending
                         select a;
            return albums;
        }
    

    在你的客户端代码中你可以这样做:

    public void LoadAlbumsWithPhotos ()
    {
      LoadOperation<Album> albumLoader = Context.Load( Context.GetAlbumsQuery() );
      albumLoader.Completed += ( s, e ) =>
        {
           _albumStore = ( s as LoadOperation<Album> ).Entities.ToList();
        };
    }
    

    然后可以通过以下方式检索照片:

    var photos = _albumStore.First().Photos.Select( p => p);
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多