【问题标题】:Entity Framework Inheritance in breezejs and ASP.NET Web Api OData微风js和ASP.NET Web Api OData中的实体框架继承
【发布时间】: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是如何定义的?您能否使用 CarVehicle 类的元数据的简短 sn-ps 更新您的问题?最终,如有必要,您可以使用客户端编码的元数据修复损坏。但如果可以的话,最好在服务器上生成正确的元数据。
  • @Ward:更新为包含示例模型的元数据。

标签: entity-framework breeze asp.net-web-api-odata


【解决方案1】:

对我来说最突出的是Cars“EntitySet”在 EDMX 概念模型中缺失,因此在 EdmBuilder 生成的元数据中缺失。 OTOH,当您致电 ODataConventionModelBuilder 时,您包括了 Cars

你说的不是同一个“模型”。

我认为(希望)如果您将Cars“EntitySet”添加到DbContext,您将获得预期的结果。

【讨论】:

  • 这个示例是模型优先的 EF 上下文,所以我猜这是设计使然。因为派生类型 (Car) 显示在 edmx 设计器上,但它不包含在 EntitySet 中。我还使用代码优先的 dbcontext 对其进行了检查。将“汽车”作为 EntitySet 包含在内无效。
  • 这没有任何意义。我认为这是一个很小的样本,我们可以轻松加载和理解。如果是这样,你会把它放在我们可以下载和尝试的地方吗?我们需要真正的代码。谢谢。
猜你喜欢
  • 1970-01-01
  • 2013-08-04
  • 2012-11-07
  • 2015-04-20
  • 2016-07-01
  • 1970-01-01
  • 2013-08-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多