2013-12-08 45 views
1

我想在編寫(1.4.6)Web API 2(ODataController)時使用最新版本的Breeze。我的問題是來自Breeze的結果缺少導航屬性。Breeze 1.4.6 + Web API 2 OData - 導航屬性丟失

考慮以下幾點:

public class Country : EntityBase<long> 
{ 
    public Country() 
    { 
     Provinces = new List<Province>(); 
    } 

    [Required] 
    [StringLength(256)] 
    public string Name { get; set; } 

    [Required] 
    [StringLength(3)] 
    public string Abbreviation { get; set; } 

    // Navigation Properties 

    [InverseProperty("Country")] 
    public virtual ICollection<Province> Provinces { get; set; } 
} 

public class Province : EntityBase<long> 
{ 
    [Required] 
    [StringLength(256)] 
    public string Name { get; set; } 

    [Required] 
    [StringLength(3)] 
    public string Abbreviation { get; set; } 

    public long CountryId { get; set; } 

    [ForeignKey("CountryId")] 
    [InverseProperty("Provinces")] 
    public virtual Country Country { get; set; } 
} 

然後我架式出ODataController使用VS 2013國家,並確保我WebApiConfig被正確的更新:

var builder = new ODataConventionModelBuilder(); 
builder.EntitySet<Country>("Countries"); 
builder.EntitySet<Province>("Provinces"); 
builder.Namespace = "MyNamespace.Models"; 

var batchHandler = new DefaultODataBatchHandler(GlobalConfiguration.DefaultServer); 
config.Routes.MapODataRoute("odata", "odata", builder.GetEdmModel(), batchHandler); 

在這一點上我能針對我的數據成功運行查詢。 ?

http://mysite.local/Api/odata/Countries $展開;各省

這將返回你所期望的:

{ 
    "odata.metadata":"http://mysite.local/Api/odata/$metadata#Countries","value":[ 
    { 
     "Provinces":[ 
     { 
      "Name":"Alberta","Abbreviation":"AB","CountryId":"1","Id":"1" 
     },{ 
      "Name":"British Columbia","Abbreviation":"BC","CountryId":"1","Id":"2" 
     },{ 
      "Name":"Manitoba","Abbreviation":"MB","CountryId":"1","Id":"3" 
     },{ 
      "Name":"New Brunswick","Abbreviation":"NB","CountryId":"1","Id":"4" 
     } 
     // edited for brevity 
     ],"Name":"Canada","Abbreviation":"CA","Id":"1" 
    } 
} 

現在我已經準備好開始消費客戶端上的這一數據。我連接datajs/Breeze並配置微風使用OData。下面是我使用的配置和創建微風經理什麼:

function configureBreezeManager() { 
     breeze.config.initializeAdapterInstances({ 
      dataService: 'OData' 
     }); 
     breeze.NamingConvention.camelCase.setAsDefault(); 
     var mgr = new breeze.EntityManager('http://mysite.local/api/odata'); 
     return mgr; 
    } 

然後我試圖做一個簡單的查詢:

var query = breeze.EntityQuery.from('Countries').expand('Province').orderBy('name'); 

此查詢成功,我也得到了幾個國家對象的結果(加拿大&美國),但它缺少省份的導航屬性。其他數據(id,名稱,縮寫)都按預期方式出現。我也嘗試了這一點,並查詢各省和擴大國家,並得到相同的結果(沒有導航屬性)。

我可以看到Breeze正確追加了$ expand,請求url是我所期望的。

我讀過OData缺少Breeze需要的外鍵信息,但Breeze 1.4.4添加了對OData v3的支持,所以我不確定這是否應該工作?

謝謝。

編輯:這是在的情況下產生的元數據可以幫助:

<edmx:Edmx xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx" Version="1.0"> 
    <edmx:DataServices xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" m:DataServiceVersion="3.0" m:MaxDataServiceVersion="3.0"> 
    <Schema xmlns="http://schemas.microsoft.com/ado/2009/11/edm" Namespace="MyNamespace.Models"> 
     <EntityType Name="Country"> 
     <Key> 
      <PropertyRef Name="Id"/> 
     </Key> 
     <Property Name="Name" Type="Edm.String" Nullable="false"/> 
     <Property Name="Abbreviation" Type="Edm.String" Nullable="false"/> 
     <Property Name="Id" Type="Edm.Int64" Nullable="false"/> 
     <NavigationProperty Name="Provinces" Relationship="MyNamespace.Models.MyNamespace_Models_Country_Provinces_MyNamespace_Models_Province_ProvincesPartner" ToRole="Provinces" FromRole="ProvincesPartner"/> 
     </EntityType> 
     <EntityType Name="Province"> 
     <Key> 
      <PropertyRef Name="Id"/> 
     </Key> 
     <Property Name="Name" Type="Edm.String" Nullable="false"/> 
     <Property Name="Abbreviation" Type="Edm.String" Nullable="false"/> 
     <Property Name="CountryId" Type="Edm.Int64" Nullable="false"/> 
     <Property Name="Id" Type="Edm.Int64" Nullable="false"/> 
     <NavigationProperty Name="Country" Relationship="MyNamespace.Models.MyNamespace_Models_Province_Country_MyNamespace_Models_Country_CountryPartner" ToRole="Country" FromRole="CountryPartner"/> 
     </EntityType> 
     <Association Name="MyNamespace_Models_Country_Provinces_MyNamespace_Models_Province_ProvincesPartner"> 
     <End Type="MyNamespace.Models.Province" Role="Provinces" Multiplicity="*"/> 
     <End Type="MyNamespace.Models.Country" Role="ProvincesPartner" Multiplicity="0..1"/> 
     </Association> 
     <Association Name="MyNamespace_Models_Province_Country_MyNamespace_Models_Country_CountryPartner"> 
     <End Type="MyNamespace.Models.Country" Role="Country" Multiplicity="0..1"/> 
     <End Type="MyNamespace.Models.Province" Role="CountryPartner" Multiplicity="0..1"/> 
     </Association> 
     <EntityContainer Name="Container" m:IsDefaultEntityContainer="true"> 
     <EntitySet Name="Countries" EntityType="MyNamespace.Models.Country"/> 
     <EntitySet Name="Provinces" EntityType="MyNamespace.Models.Province"/> 
     <AssociationSet Name="MyNamespace_Models_Country_Provinces_MyNamespace_Models_Province_ProvincesPartnerSet" Association="MyNamespace.Models.MyNamespace_Models_Country_Provinces_MyNamespace_Models_Province_ProvincesPartner"> 
      <End Role="ProvincesPartner" EntitySet="Countries"/> 
      <End Role="Provinces" EntitySet="Provinces"/> 
     </AssociationSet> 
     <AssociationSet Name="MyNamespace_Models_Province_Country_MyNamespace_Models_Country_CountryPartnerSet" Association="MyNamespace.Models.MyNamespace_Models_Province_Country_MyNamespace_Models_Country_CountryPartner"> 
      <End Role="CountryPartner" EntitySet="Provinces"/> 
      <End Role="Country" EntitySet="Countries"/> 
     </AssociationSet> 
     </EntityContainer> 
    </Schema> 
    </edmx:DataServices> 
</edmx:Edmx> 

回答

1

這裏的問題是,微軟的ODataConventionModelBuilder目前不暴露於EDMX支持模型的OData約束。他們知道這一點,並聲稱他們計劃在稍後的一些版本中這樣做。

在此期間,您有幾個選項。

1)忽略ODataConventionBuilder並使用默認的Breeze WebApi2實現直接與包裝EFModel的BreezeController的實例對話。這實際上支持ODataConventionModelBuilder提供的超集,而沒有任何額外的複雜性。查看Breeze zip中的任何Breeze示例。 (尤其是DocCode示例)。

2)使用WCF DataService將您的EF模型公開爲OData服務。 WCF DataServices確實暴露了OData限制。

我們正計劃在「將來」的某個時間提供有關您在這些區域中每個區域的選項的其他文檔。所以請繼續關注。

+0

有沒有人研究過擴展ODataConventionBuilder以支持這些約束?我真的希望堅持使用網絡API,並使用OData。 –

+0

我們已經討論過一種方法,允許您手動「調整」breeze客戶端數據模型以允許這樣做,但我們實際上並未考慮修改ODataConventionModelBuilder。但這是一個有趣的方法。我們希望MS在我們之前到達那裏。 –