2012-06-11 66 views
0

當我將BarCodeId FK更改爲在ContactDocument中允許爲null時。之後,如果ContactDocument中的任何實體具有null BarCodeId,我將開始在客戶端中發生錯誤。如果我刪除了最後三次展開,所有將是工作,但我不會得到有關BarCode對象的實體。任何幫助?WCF數據服務|如何處理帶有嵌套屬性的可空屬性?

IQueryable<ScanDocument> result = Context.CreateQuery<ScanDocument>("DocumnetsWithNotRemovedContacts") 
     .Expand(ScanDocument.ScanDocumentDetailsPropertyName) 
     .Expand(ScanDocument.ScanDocumentDetailsPropertyName + "/" + ScanDocumentDetail.ScanDocumentPropertyName) 
     .Expand(ScanDocument.ScanDocumentDetailsPropertyName + "/" + ScanDocumentDetail.ContactDocumentsPropertyName) 
     .Expand(ScanDocument.ScanDocumentDetailsPropertyName + "/" + ScanDocumentDetail.ContactDocumentsPropertyName + "/" + ContactDocument.BarCodePropertyName) 
     .Expand(ScanDocument.ScanDocumentDetailsPropertyName + "/" + ScanDocumentDetail.ContactDocumentsPropertyName + "/" + ContactDocument.ContactPropertyName + "/" + Contact.ContactDocumentsPropertyName) 
     .Expand(ScanDocument.ScanDocumentDetailsPropertyName + "/" + ScanDocumentDetail.ContactDocumentsPropertyName + "/" + ContactDocument.ScanDocumentDetailPropertyName) 
     .Expand(ScanDocument.ScanDocumentDetailsPropertyName + "/" + ScanDocumentDetail.ContactDocumentsPropertyName + "/" + ContactDocument.ContactPropertyName + "/" + Contact.ContactSourcePropertyName) 
     .Expand(ScanDocument.ScanDocumentDetailsPropertyName + "/" + ScanDocumentDetail.ContactDocumentsPropertyName + "/" + ContactDocument.ContactPropertyName + "/" + Contact.ElectionDistrictPropertyName) 
     .Expand(ScanDocument.ScanDocumentDetailsPropertyName + "/" + ScanDocumentDetail.ContactDocumentsPropertyName + "/" + ContactDocument.ContactPropertyName + "/" + Contact.ContactTypePropertyName) 
     .Expand(ScanDocument.ScanDocumentDetailsPropertyName + "/" + ScanDocumentDetail.ContactDocumentsPropertyName + "/" + ContactDocument.ContactPropertyName + "/" + Contact.ContactSalutationPropertyName) 
     .Expand(ScanDocument.ScanDocumentDetailsPropertyName + "/" + ScanDocumentDetail.ContactDocumentsPropertyName + "/" + ContactDocument.ContactPropertyName + "/" + Contact.ContactGroupsPropertyName) 
     .Expand(ScanDocument.ScanDocumentDetailsPropertyName + "/" + ScanDocumentDetail.ContactDocumentsPropertyName + "/" + ContactDocument.ContactPropertyName + "/" + Contact.CategoryInterestsPropertyName) 
     .Expand(ScanDocument.ScanDocumentDetailsPropertyName + "/" + ScanDocumentDetail.ContactDocumentsPropertyName + "/" + ContactDocument.BarCodePropertyName + "/" + BarCode.ContactDocumentsPropertyName) 
     .Expand(ScanDocument.ScanDocumentDetailsPropertyName + "/" + ScanDocumentDetail.ContactDocumentsPropertyName + "/" + ContactDocument.BarCodePropertyName + "/" + BarCode.BarCodeActionTypePropertyName) 
     .Expand(ScanDocument.ScanDocumentDetailsPropertyName + "/" + ScanDocumentDetail.ContactDocumentsPropertyName + "/" + ContactDocument.BarCodePropertyName + "/" + BarCode.ScanDataTypePropertyName) 
     .OrderByDescending(document => document.ID); 

在服務器端我開始使用類似這樣的東西來獲取嵌套屬性。

CurrentDataSource.ScanDocumentDetails 
      .Include(ScanDocumentDetail.ScanDocumentPropertyName) 
      .Include(ScanDocumentDetail.ContactDocumentsPropertyName) 
      .Include(ScanDocumentDetail.ContactDocumentsPropertyName + "." + ContactDocument.BarCodePropertyName) 
      .Include(ScanDocumentDetail.ContactDocumentsPropertyName + "." + ContactDocument.ContactPropertyName + "." + Contact.ContactDocumentsPropertyName) 
      .Include(ScanDocumentDetail.ContactDocumentsPropertyName + "." + ContactDocument.ScanDocumentDetailPropertyName) 
      .Include(ScanDocumentDetail.ContactDocumentsPropertyName + "." + ContactDocument.ContactPropertyName + "." + Contact.ContactSourcePropertyName) 
      .Include(ScanDocumentDetail.ContactDocumentsPropertyName + "." + ContactDocument.ContactPropertyName + "." + Contact.ElectionDistrictPropertyName) 
      .Include(ScanDocumentDetail.ContactDocumentsPropertyName + "." + ContactDocument.ContactPropertyName + "." + Contact.ContactTypePropertyName) 
      .Include(ScanDocumentDetail.ContactDocumentsPropertyName + "." + ContactDocument.ContactPropertyName + "." + Contact.ContactSalutationPropertyName) 
      .Include(ScanDocumentDetail.ContactDocumentsPropertyName + "." + ContactDocument.ContactPropertyName + "." + Contact.ContactGroupsPropertyName) 
      .Include(ScanDocumentDetail.ContactDocumentsPropertyName + "." + ContactDocument.ContactPropertyName + "." + Contact.CategoryInterestsPropertyName) 
      //.Include(ScanDocumentDetail.ContactDocumentsPropertyName + "." + ContactDocument.BarCodePropertyName + "." + BarCode.ContactDocumentsPropertyName) 
      //.Include(ScanDocumentDetail.ContactDocumentsPropertyName + "." + ContactDocument.BarCodePropertyName + "." + BarCode.BarCodeActionTypePropertyName) 
      //.Include(ScanDocumentDetail.ContactDocumentsPropertyName + "." + ContactDocument.BarCodePropertyName + "." + BarCode.ScanDataTypePropertyName) 
      .Where(c => ids.Contains(c.ScanDocumentID ?? 0)).ToList(); 

     foreach (var scanDocumentDetail in y) 
     { 
      foreach (var contactdocuments in scanDocumentDetail.ContactDocuments) 
      { 
       if (contactdocuments.BarCode != null) 
       { 
        int barCodeActionTypeId = contactdocuments.BarCode.BarCodeActionTypeId; 
        contactdocuments.BarCode.BarCodeActionType = 
         CurrentDataSource.BarCodeActionTypes.FirstOrDefault(p => p.ID == barCodeActionTypeId); 

        if (contactdocuments.BarCode.ScanDataTypeID.HasValue) 
        { 
         int scanDataTypeId = contactdocuments.BarCode.ScanDataTypeID.Value; 
         contactdocuments.BarCode.ScanDataType = 
          CurrentDataSource.ScanDataTypes.FirstOrDefault(p => p.ID == scanDataTypeId); 
        } 
       } 
      } 
     } 

UPD 例外: 「錯誤處理響應流服務器失敗,以下消息:\ r \處理此請求時發生錯誤楠」 來源:System.Data.Services.Client 狀態代碼:500

堆棧跟蹤:

at System.Data.Services.Client.Xml.XmlAtomErrorReader.Read() 
    at System.Xml.XmlSubtreeReader.Read() 
    at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r) 
    at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r, LoadOptions o) 
    at System.Xml.Linq.XElement.ReadElementFrom(XmlReader r, LoadOptions o) 
    at System.Xml.Linq.XElement.Load(XmlReader reader, LoadOptions options) 
    at System.Data.Services.Client.AtomParser.XElementBuilderCallback(XmlReader reader) 
    at System.Data.Services.Client.AtomParser.ParseCurrentEntry(AtomEntry& targetEntry) 
    at System.Data.Services.Client.AtomParser.Read() 
    at System.Data.Services.Client.AtomMaterializer.Read() 
    at System.Data.Services.Client.MaterializeAtom.MoveNextInternal() 
    at System.Data.Services.Client.MaterializeAtom.MoveNext() 
    at System.Linq.Enumerable.<CastIterator>d__b1`1.MoveNext() 
    at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) 
    at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) 
    at ClientDataServices.MailProcessingManagmentService.GetDocuments(Expression`1 filter) in D:\...\MailProcessingManagmentService.cs:line 41 
    at ClientDataServices.MailProcessingManager.GetEntries(Expression`1 predicate) in D:\...\MailProcessingManager.cs:line 24 
    at MailProcessingModule.ViewModels.MailProcessingViewModel.RefreshEntityHandler() in D:\...\MailProcessingViewModel.cs:line 113 
    at BaseModule.BaseGridViewModel`1.<>c__DisplayClass1a.<ListEntities>b__d(Object e) in D:\...\BaseGridViewModel.cs:line 178 
    at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) 
    at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() 
    at System.Threading.ThreadPoolWorkQueue.Dispatch() 
    at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback() 

回答

0

WCF數據服務對允許的擴展數的服務器端限制。我認爲它是14,所以你上面的查詢可能會失敗。

如果您將發佈確切的錯誤消息,您可以得到它可能有助於確認問題的根本原因。

展開爲null的屬性應該可以正常工作,不會有任何結果,但這是可以預料的。

+0

添加異常。 – Sergii

+0

您可以按照以下說明進行操作:http://blogs.msdn.com/b/phaniraj/archive/2008/06/18/debugging-ado-net-data-services.aspx,以啓用詳細的錯誤消息(don'不要忘記把它們轉化爲生產)併發布你得到的錯誤信息。或者,將一個調試器附加到該服務,並讓它停止所有例外。這個錯誤將作爲一個看起來很有趣的異常拋出。 –