在Silverlight错误中查询WCF数据服务中的数据

时间:2012-02-27 22:23:46

标签: c# wcf silverlight web-services ado.net

我有一个ADO.NET模型实体,它反映了一个oracle数据库和一个提供对这个ADO.NET模型的访问的WCF服务。在我的WCF服务代码中,我有以下内容:

config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
//config.SetServiceOperationAccessRule("MyServiceOperation",ServiceOperationRights.All);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;

我在silverlight 5应用程序中添加了对此服务的引用。然后在下面的代码中,我收到一个错误:

Uri WCFUri = new Uri(HtmlPage.Document.DocumentUri, "WcfDataService.svc");
WCF_Service.Entities Database = new WCF_Service.Entities(WCFUri);

var buildings = from building in Config.Database.BUILDINGs
            select building.BLDG_ID;

var buildingsQuery = (DataServiceQuery<string>)buildings;

buildingsQuery.BeginExecute(buildingsResult =>
{
    foreach (string buildingId in buildingsQuery.EndExecute(buildingsResult))
        BuildingsList.Items.Add(new ListItem(buildingId.Trim(), false));
    BuildingListBusyIndicator.IsBusy = false;
}, null);

错误是:

Navigation properties can only be selected from a single resource. Specify a key predicate to restrict the entity set to a single instance.

我对这个问题很遗憾。提前谢谢。

1 个答案:

答案 0 :(得分:0)

问题是 ADO.NET数据服务不支持投影功能。

您必须选择实体,而不是选择属性。

<强> WRONG

var buildings = from building in Config.Database.BUILDINGs
            select building.BLDG_ID;

<强> CORRECT

var buildings = from building in Config.Database.BUILDINGs
            select building;

(据我所知,在你的情况下,这种修正毫无意义,但技术上确实如此)

OR 您可以使用shell,例如:

var buildings = from building in Config.Database.BUILDINGs
            select new { Id = building.BLDG_ID };