【发布时间】:2014-04-05 05:26:33
【问题描述】:
根据this sample,我正在使用带有WebApi OData 的breathjs。如本文所述,由于缺少外键信息,我无法使用 ODataConventionModelBuilder。假设我有一个名为“Car”的实体,该实体派生自一个名为“Vehicle”的实体。使用 ODataConventionModelBuilder,我可以定义如下模型:
var builder = new ODataConventionModelBuilder();
builder.EntitySet<Vehicle>("Vehicles");
builder.EntitySet<Car>("Cars");
我可以这样查询:
- /odata/Vehicles --> 返回所有车辆。
- /odata/Cars --> 返回 只是汽车。
但是对于 Breeze EdmBuilder 类,我只能使用“/odata/Vehicles”查询。 “/odata/Cars”会导致“404 not found”错误。
似乎在使用“ODataConventionModelBuilder”时,“汽车”实体集是在元数据中定义的,但在使用轻量级 EdmBuilder 时,却不是。我可以在向元数据端点('odata/$metadata')发送请求时确认此行为。 this question 中所述的代码优先和模型优先方法都会发生这种情况。
简而言之,在微风和Web Api OData中使用EdmBuilder类时如何使用继承。
更新
这是使用“ODataConventionModelBuilder”时的元数据:
<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="EFTest.Models">
<EntityType Name="Vehicle">
<Key>
<PropertyRef Name="VehicleId"/>
</Key>
<Property Name="VehicleId" Type="Edm.Int32" Nullable="false"/>
<Property Name="Name" Type="Edm.String"/>
</EntityType>
<EntityType Name="Car" BaseType="EFTest.Models.Vehicle">
<Property Name="Capacity" Type="Edm.Int32" Nullable="false"/>
</EntityType>
</Schema>
<Schema xmlns="http://schemas.microsoft.com/ado/2009/11/edm" Namespace="Default">
<EntityContainer Name="Container" m:IsDefaultEntityContainer="true">
<EntitySet Name="Vehicles" EntityType="EFTest.Models.Vehicle"/>
<EntitySet Name="Cars" EntityType="EFTest.Models.Car"/>
</EntityContainer>
</Schema>
</edmx:DataServices>
</edmx:Edmx>
这里是使用微风 EdmBuilder 类时的元数据:
<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">
<Schema xmlns="http://schemas.microsoft.com/ado/2009/11/edm" Namespace="TestDBModel">
<EntityType Name="Car" BaseType="TestDBModel.Vehicle">
<Property Name="Capacity" Type="Edm.Int32" Nullable="false"/>
</EntityType>
<EntityType Name="Vehicle">
<Key>
<PropertyRef Name="VehicleId"/>
</Key>
<Property xmlns:p6="http://schemas.microsoft.com/ado/2009/02/edm/annotation" Name="VehicleId" Type="Edm.Int32" Nullable="false" p6:StoreGeneratedPattern="Identity"/>
<Property Name="Name" Type="Edm.String" Nullable="false" MaxLength="50" FixedLength="false" Unicode="true"/>
</EntityType>
<EntityContainer xmlns:p5="http://schemas.microsoft.com/ado/2009/02/edm/annotation" Name="TestDBEntities" p5:LazyLoadingEnabled="true">
<EntitySet Name="Vehicles" EntityType="TestDBModel.Vehicle"/>
</EntityContainer>
</Schema>
</edmx:DataServices>
</edmx:Edmx>
这里是 edmx 文件中的概念模型部分:
<edmx:ConceptualModels>
<Schema Namespace="TestDBModel" Alias="Self" annotation:UseStrongSpatialTypes="false" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2009/11/edm">
<EntityType Name="Car" BaseType="TestDBModel.Vehicle">
<Property Name="Capacity" Type="Int32" Nullable="false" />
</EntityType>
<EntityType Name="Vehicle">
<Key>
<PropertyRef Name="VehicleId" />
</Key>
<Property Name="VehicleId" Nullable="false" annotation:StoreGeneratedPattern="Identity" Type="Int32" />
<Property Name="Name" Type="String" MaxLength="50" FixedLength="false" Unicode="true" Nullable="false" />
</EntityType>
<EntityContainer Name="TestDBEntities" annotation:LazyLoadingEnabled="true">
<EntitySet Name="Vehicles" EntityType="Self.Vehicle" />
</EntityContainer>
</Schema>
</edmx:ConceptualModels>
【问题讨论】:
-
元数据中
Car是如何定义的?您能否使用Car和Vehicle类的元数据的简短 sn-ps 更新您的问题?最终,如有必要,您可以使用客户端编码的元数据修复损坏。但如果可以的话,最好在服务器上生成正确的元数据。 -
@Ward:更新为包含示例模型的元数据。
标签: entity-framework breeze asp.net-web-api-odata