exposing Associated entities in ria services

只谈情不闲聊 提交于 2019-12-25 04:29:31

问题


I have read this link :-

RIA Services: Entity Framework Reference Entities

and added [Include] in metadata class as well as in my domain service query.

On my client side i have a navigation property named Photos in my Albums entity. However it is of type EntityCollection and i don't know how to iterate over it. It has no get method and even indexing doesn't apply on it. I have tried the following :-

albums.photos[0]

but it doesn't work. Can anybody tell me how do i iterate over that photos collection in my albums entity ?

Thanks in advance :)


回答1:


So, in the metadata for your Albums class you have something like:

[MetadataTypeAttribute( typeof(Album.AlbumMetadata ) )]
public partial class Album
{
  internal sealed class AlbumMetadata
  {
    private AlbumMetadata ()
    { }

    [Include]
    public EntityCollection<Photo> Photos { get; set; } 
}

In your domain service you would have something like:

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

In you clientside code you could then do:

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

Photos could then be retrieved by:

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

Hope this helps.



来源:https://stackoverflow.com/questions/4853662/exposing-associated-entities-in-ria-services

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!