我正在将 RIA 服务与 Silverlight 4 结合使用,并希望限制从该服务返回的字段。例如:
TableA:
ID
Field1
Field2
Field3
TableB:
ID
TableAID (foreign key)
Field1
RestrictedField2
在我的域服务类中,我在创建服务时生成了类似的内容。我添加了包含(工作正常):
<RequiresAuthentication()>
Public Function GetTableA() As IQueryable(Of TableA)
Return Me.ObjectContext.TableA.Include("TableB")
End Function
我的问题是,如何从 TableA 中获取所有列,并从 TableB 中获取 Field1 而不返回 RestrictedField2?我很确定这是通过一些 Linq 奇思妙想来完成的,但我不太确定如何实现。
谢谢!
马特
更新
我上面没有列出的一项要求。 必须在服务器端删除该列,因为 RestrictedField1 中的数据无法发送到客户端。另外,我需要在不同的域服务方法中使用此字段(受 RequiresRoleAttribute 保护),以便我可以将信息公开给管理员。此要求意味着我不想创建不同的复杂类型并返回它。我更愿意继续使用 EF 模型类型。
I'm using RIA services with Silverlight 4 and would like to limit the fields that are returned from the service. For example:
TableA:
ID
Field1
Field2
Field3
TableB:
ID
TableAID (foreign key)
Field1
RestrictedField2
In my domain service class I have something like this that was generated when I created the service. I added the includes (which are working fine):
<RequiresAuthentication()>
Public Function GetTableA() As IQueryable(Of TableA)
Return Me.ObjectContext.TableA.Include("TableB")
End Function
My question is, how do I get all of the columns from TableA and also get Field1 from TableB without returning the RestrictedField2? I'm pretty sure this is done through some Linq fanciness, but I'm not quite sure how.
Thanks!
Matt
Update
One requirement that I didn't list above. The column must be removed on the server side as the data in RestrictedField1 cannot have any chance of being sent to the client. Also, I will need to use this field in a different domain service method (protected with RequiresRoleAttribute), so I can expose the information to an administrator. This requirement means that I don't want to create a different complex type and return that. I would prefer to continue working with the EF model type.
发布评论
评论(2)
检查此链接,我认为它可以解决您的问题,而不需要视图模型
http://social.msdn.microsoft.com/Forums/en/adodotnetentityframework/thread/ab7b251a-ded0-487e-97a9-
我看来你可以返回一个匿名类型,然后将其转换为您需要的类型。
Check this link, I think it may solve your problem without the need of a view model
http://social.msdn.microsoft.com/Forums/en/adodotnetentityframework/thread/ab7b251a-ded0-487e-97a9-
I appears you can return an anonymous type then convert it to your needed type.
根据我发现的一些信息,完成我需要的最好方法是在数据库中创建一个视图并通过 EF 和 RIA 服务公开我需要的数据。这似乎是可用的最佳解决方案。
Based on some information that I found, the best way to accomplish what I need is to create a view in the database and expose the data I need via EF and RIA Services. This appears to be the best solution available.