2012-02-27 76 views
1

我有一個ADO.NET模型實體,它反映了一個Oracle數據庫和一個提供對此ADO.NET模型的訪問的WCF服務。在我的WCF服務代碼中,我有以下內容:從Silverlight中的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. 

我失去了什麼問題。提前致謝。

+0

您會在哪一行發生此錯誤? – 2012-02-27 22:53:44

+0

我現在不在我的電腦上工作,但我認爲這是 var buildings =從建築物的Config.Database.BUILDINGs select building.BLDG_ID; – Justin 2012-02-28 00:33:33

回答

0

問題是,ADO.NET數據服務不支持投影功能。

您不必選擇屬性,而必須選擇實體。

WRONG

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

正確

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

(據我所知,在你的情況下,這個校正毫無意義,但在技術上這是真的)

您可以使用外殼,例如:

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