简体   繁体   English

breezejs和ASP.NET Web Api OData中的实体框架继承

[英]Entity Framework Inheritance in breezejs and ASP.NET Web Api OData

I'm using breezejs with WebApi OData according to this sample . 根据此示例,我将breezejs与WebApi OData一起使用。 As stated in this article I cannot use ODataConventionModelBuilder because of the missing foreign key information. 如本文所述,由于缺少外键信息,因此无法使用ODataConventionModelBuilder。 Suppose I have an entity named 'Car' which is derived from an entity named 'Vehicle'. 假设我有一个名为“ Car”的实体,它是从一个名为“ Vehicle”的实体派生的。 With ODataConventionModelBuilder I can define a model such as this: 使用ODataConventionModelBuilder,我可以定义一个这样的模型:

var builder = new ODataConventionModelBuilder();
builder.EntitySet<Vehicle>("Vehicles");
builder.EntitySet<Car>("Cars");

I can do the query like : 我可以像这样进行查询:

  • /odata/Vehicles --> returns all the vehicles. / odata / Vehicles->返回所有车辆。
  • /odata/Cars --> returns just the cars. / odata / Cars->仅返回汽车。

But with Breeze EdmBuilder class I can only use the '/odata/Vehicles' query. 但是对于Breeze EdmBuilder类,我只能使用“ / odata / Vehicles”查询。 The '/odata/Cars' would result in a '404 not found' error. “ / odata /汽车”将导致“ 404未找到”错误。

It seems that when using 'ODataConventionModelBuilder', the 'Cars' entity set is defined in the metadata but when using the breeze EdmBuilder, it is not. 似乎在使用“ ODataConventionModelBuilder”时,“ Cars”实体集是在元数据中定义的,而在使用微风的EdmBuilder时则不是。 I can confirm this behavior when sending request to the metadata end point('odata/$metadata'). 将请求发送到元数据端点('odata / $ metadata')时,我可以确认此行为。 This happens for both code first and model first approaches stated in this question . 对于此问题中所述的代码优先和模型优先方法,都是这样

In short, how do I use inheritance when using EdmBuilder class in breeze and Web Api OData. 简而言之,在微风和Web Api OData中使用EdmBuilder类时,如何使用继承。

UPDATED 更新

here is the metadata when using 'ODataConventionModelBuilder': 这是使用“ 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>

here is metadata when using breeze EdmBuilder class: 这是使用微风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>

here is the conceptual model section in the edmx file: 这是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>

What stands out for me is that the Cars "EntitySet" is missing from the EDMX conceptual model and, consequently, from the metadata generated by EdmBuilder. 对我而言突出的是,EDMX概念模型以及因此EdmBuilder生成的元数据中缺少Cars “ EntitySet”。 OTOH, you included Cars when you called ODataConventionModelBuilder . OTOH,您在调用ODataConventionModelBuilder时包括了Cars

You aren't really talking about the same "model". 您并不是真正在谈论相同的“模型”。

I think (hope) you will get the desired outcome if you add the Cars "EntitySet" to the DbContext . 我认为(希望)如果将Cars “ EntitySet”添加到DbContext您将获得期望的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM