2016-11-18 25 views
0

我有以下型號:如何包括只讀在OData的查詢屬性

public class Employee 
{ 
    public int EmployeeId { get; set; } 

    public string Name { get; set; } 

    [...] 

    public int OfficeId { get; set; }  

    public string OfficeInfo 
    { 
    get { return Office.Info; } 
    } 

    public Office Office { get; set; } 
} 

public class Office 
{ 
    public int OfficeId { get; set; } 

    public string Info { get; set; } 
} 

我在客戶端的網格我想與員工的情況下喂,包括OfficeInfo其中行其中一列,所以我消費它通過以下查詢:

「/?的OData /員工$展開;辦公室& $選擇=僱員,名稱,OfficeInfo」

我已經註冊了兩個實體IEdmModel:

private static IEdmModel GetEDMModel() 
{ 
    ODataModelBuilder builder = new ODataConventionModelBuilder(); 

    builder.EntitySet<Employee>("Employees"); 
    builder.EntitySet<Office>("Offices"); 

    [...] 
} 

和我的獲取動作看起來是這樣的:

[EnableQuery] 
public IQueryable<Employees> Get() 
{ 
    [...] 
} 

,但我不斷收到此異常:

「找不到一個叫上鍵入‘OfficeInfo’「屬性Xds.Entities.Employee '「

我在這裏錯過了什麼?

+0

我知道它不會回答你的問題問題,但有沒有什麼阻止你使用'Employee.Office.Info'來代替? – smoksnes

回答

0

您可以檢查模型元數據,看看是否在'Xds.Entities.Employee'類型下面顯示。

<Property Name="OfficeInfo" Type="Edm.String" /> 

由於它是一個只讀屬性,你應該打開isQueryCompositionMode到模型中得到顯示它,像(可以通過有真正HttpConfiguration):

ODataModelBuilder builder = new ODataConventionModelBuilder(new System.Web.Http.HttpConfiguration(), true); 

之後,查詢應該工作。

請注意,標記標記爲用於測試目的,但如果您手動驗證元數據應該沒問題。

0

你可以標記屬性OfficeInfo要求或明確地添加該屬性:

  • 根據需要注意到:

    builder 
         .EntitySet<Employee>("Employees") 
         .EntityType 
         .Property(_ => _.OfficeInfo) 
         .IsRequired(); 
    
  • 添加明確:

    builder 
         .EntitySet<Employee>("Employees") 
         .EntityType 
         .Property(_ => _.OfficeInfo) 
         .AddedExplicitly = true; 
    
+0

應用您的解決方案後,現在我收到了另一個錯誤: System.NotSupportedException LINQ to Entities不支持指定的類型成員'OfficeInfo'。僅支持初始化程序,實體成員和實體導航屬性。 – slashCoder

+0

@slashCoder我假設'OfficeInfo'這個列被用在'''​​Employees.Where(_ => _.OfficeInfo ==「?」)或'employees.OrderBy(_ => _.OfficeInfo)'。這是不允許的。而是使用像'employees.Where(_ => _.Office.Info ==「?」)''這樣的導航屬性'Office'。 –