如何使用 Ria 服务自动加载与实体相关的详细信息(带条件)?
我正在使用 Silverlight 4 和 Entity Framework 4 开发一个项目,并且尝试在客户端加载 EntityQuery 时自动加载与实体关联的详细信息(带条件)。
到目前为止,我已经能够使用 Include 属性制定一个解决方案,该解决方案返回与主实体关联的所有详细信息。我在这里缺少的是能够根据某些标准过滤掉详细信息。
作为示例,我的实体如下所示:
实体 Movie
Id (int)
[Include]
MovieLocalizedInformations (EntityCollection<MovieLocalizedInformation>)
实体 MovieLocalizedInformation
Id (int)
Movie_Id (int)
LanguageCode (eg.: en)
Title
在我的 DomainService 对象上,我公开了以下方法:
public IQueryable<Movie> GetMovies( string languageCode )
{
return this.ObjectContext.Movies.Include( "MovieLocalizedInformations" );
}
这工作正常。但是,当我尝试添加 where 子句来根据语言代码过滤掉本地化信息时,只有电影会加载到客户端上。
有没有一种方法可以在一个查询中实现过滤?
注意:我还在客户端上使用 DomainDataSource 和分页,因此解决方案需要与之配合。
任何帮助将不胜感激!
谢谢,
雅克。
I'm developing a project using Silverlight 4 and Entity Framework 4 and I'm trying to auto-load the details (with conditions) associated with an entity when the client loads the EntityQuery.
So far, I've been able to put in place a solution, using the Include attribute, that returns all the details associated with the master entity. What I'm missing here is to be able to filter out the details based on some criteria.
As an example, here's what my entities look like:
Entity Movie
Id (int)
[Include]
MovieLocalizedInformations (EntityCollection<MovieLocalizedInformation>)
Entity MovieLocalizedInformation
Id (int)
Movie_Id (int)
LanguageCode (eg.: en)
Title
On my DomainService object, I expose the following method:
public IQueryable<Movie> GetMovies( string languageCode )
{
return this.ObjectContext.Movies.Include( "MovieLocalizedInformations" );
}
This works fine. But when I try to add where clause to filter out the localized information based on the language code, only the movies get loaded on the client.
Is there a way to achieve the filtering in one query?
Note: I'm also using the DomainDataSource with paging on the client so the solution needs to work with that.
Any help would be greatly appreciated!
Thanks,
Jacques.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定实体框架,但对于 LinqToSqlDomainService,您可以使用 LoadWith loadOption
包含详细信息实体,然后使用 AssociateWith LoadOption 来过滤详细信息,例如
Not sure about Enitity Framework but with a LinqToSqlDomainService you use the LoadWith loadOption
to include the details entities and then use the AssociateWith LoadOption to filter the detail e.g
好的,
出于效率原因,我决定使用自定义 DTO 对象来获取本地化信息并展平结果。
但是,当我的自定义 DTO 需要引用另一个自定义本地化 DTO 时,会出现同样的问题。
以下是我如何执行与 ObjectSet 提供的 .Include( "PropertyName" ) 相同的操作:
Entity LocalizedMovieCollection
Entity LocalizedMovie
然后,我声明了两个方法:一个返回 LocalizedMovieCollection 的 IQueryable,另一个返回 LocalizedMovie 的 IQueryable。 (注意:必须至少有一个方法返回每种类型的实体,以便在 Silverlight 客户端上自动生成实体)
我的目标是自动加载与电影关联的 MovieCollection,因此获取电影的方法定义是如下所示:
当 Silverlight 客户端加载查询时,所有 LocalizedMovies 及其关联的 LocalizedMovieCollections 将被加载到上下文中。
Ok,
For efficiency reason, I decided to go with custom DTO object that fetches the localized information and flatten the result.
But, the same problem occurred when my custom DTO needed to reference another custom localized DTO.
Here is how I came to do the same as the .Include( "PropertyName" ) that the ObjectSet offers:
Entity LocalizedMovieCollection
Entity LocalizedMovie
Then, I've declared two methods: One that returns an IQueryable of LocalizedMovieCollection and the other, an IQueryable of LocalizedMovie. (Note: There must be at least one method that returns each type of entity for the entity to get auto-generated on the Silverlight client)
My goal is to automatically load the MovieCollection associated with a Movie so the method definition to get the movies is as follow:
When the Silverlight client loads the query, all the LocalizedMovies and their associated LocalizedMovieCollections will be loaded into the context.