【发布时间】:2013-12-25 23:30:38
【问题描述】:
在撰写本文时,我正在尝试将最新版本的 Breeze (1.4.6) 与 Web API 2 (ODataController) 一起使用。我的问题是 Breeze 的结果缺少导航属性。
考虑以下几点:
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; }
}
然后我使用 VS 2013 为 Country 搭建了一个 ODataController 并确保我的 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?$expand=省
这会返回你所期望的:
{
"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、名称、缩写)都按预期存在。我还尝试了与此相反的操作,并查询 Provinces 并扩展 Country 并得到相同的结果(无导航属性)。
我可以看到 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>
【问题讨论】:
标签: asp.net-web-api odata breeze