根据此示例,我正在使用带有 WebApi OData 的微风。如本文所述,由于缺少外键信息,我无法使用 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')发送请求时确认此行为。此问题中所述的代码优先和模型优先方法都会发生这种情况。
简而言之,在微风和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>