Breeze working with Web API OData

我怕爱的太早我们不能终老 提交于 2019-12-11 13:35:38

问题


I have a .NET WebAPI 2 Odata service and i am working with Breeze on top of it The OData service is based on the VS2013 ASP.Net VNext version The service supports expand I managed to trick the Microsoft OData metadata serialize to provide the referential constraint with the foreign key like so:

<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="ODataGame.Models">
<EntityType Name="Incident">
<Key>
<PropertyRef Name="ID"/>
</Key>
<Property Name="ID" Type="Edm.Int32" Nullable="false"/>
<Property Name="Name" Type="Edm.String"/>
<Property Name="Desc" Type="Edm.String"/>
<NavigationProperty Name="DTask" Relationship="ODataGame.Models.ODataGame_Models_DTask_DTaskPartner_ODataGame_Models_Incident_DTask" ToRole="DTask" FromRole="Incident"/>
</EntityType>
<EntityType Name="DTask">
<Key>
<PropertyRef Name="ID"/>
</Key>
<Property Name="ID" Type="Edm.Int32" Nullable="false"/>
<Property Name="Name" Type="Edm.String"/>
<Property Name="IncidentID" Type="Edm.Int32" Nullable="false"/>
<NavigationProperty Name="Incident" Relationship="ODataGame.Models.ODataGame_Models_DTask_DTaskPartner_ODataGame_Models_Incident_DTask" ToRole="Incident" FromRole="DTask"/>
</EntityType>
<Association Name="ODataGame_Models_DTask_DTaskPartner_ODataGame_Models_Incident_DTask">
<End Type="ODataGame.Models.Incident" Role="Incident" Multiplicity="0..1"/>
<End Type="ODataGame.Models.DTask" Role="DTask" Multiplicity="*"/>
<ReferentialConstraint>
<Principal Role="Incident">
<PropertyRef Name="ID"/>
</Principal>
<Dependent Role="DTask">
<PropertyRef Name="IncidentID"/>
</Dependent>
</ReferentialConstraint>
</Association>
<Association Name="ODataGame_Models_DTask_Incident_ODataGame_Models_Incident_IncidentPartner">
<End Type="ODataGame.Models.Incident" Role="Incident" Multiplicity="0..1"/>
<End Type="ODataGame.Models.DTask" Role="DTask" Multiplicity="0..1"/>
</Association>
<EntityContainer Name="Container" m:IsDefaultEntityContainer="true">
<EntitySet Name="Incident" EntityType="ODataGame.Models.Incident"/>
<EntitySet Name="DTask" EntityType="ODataGame.Models.DTask"/>
<AssociationSet Name="ODataGame_Models_DTask_DTaskPartner_ODataGame_Models_Incident_DTaskSet" Association="ODataGame.Models.ODataGame_Models_DTask_DTaskPartner_ODataGame_Models_Incident_DTask">
<End Role="Incident" EntitySet="Incident"/>
<End Role="DTask" EntitySet="DTask"/>
</AssociationSet>
<AssociationSet Name="ODataGame_Models_DTask_DTaskPartner_ODataGame_Models_Incident_DTaskSet" Association="ODataGame.Models.ODataGame_Models_DTask_DTaskPartner_ODataGame_Models_Incident_DTask">
<End Role="DTask" EntitySet="DTask"/>
<End Role="Incident" EntitySet="Incident"/>
</AssociationSet>
</EntityContainer>
</Schema>
</edmx:DataServices>
</edmx:Edmx>

The problem is that the web API odata returns expand result within an extra result element and not directly as a jason array like so:

     "__metadata":{
              "id":"http://localhost:27698/odata/Incident(3)","uri":"http://localhost:27698/odata/Incident(3)","type":"ODataGame.Models.Incident"
            },"DTask":{
              "results":[
                {
                  "__metadata":{
                    "id":"http://localhost:27698/odata/DTask(1)","uri":"http://localhost:27698/odata/DTask(1)","type":"ODataGame.Models.DTask"
                  },"Incident":{
                    "__deferred":{
                      "uri":"http://localhost:27698/odata/DTask(MEIR%20MISSING)/Incident"
                    }
                  },"ID":1,"Name":"kk","IncidentID":3
                }
              ]
            },"ID":3,"Name":"asas","Desc":"zz"
          }

is there a way to configure breeze to handle that correctly?

If i have a navigation property only within one element without a reverse property on the other side, for example in my case an incident holding a collection of task but a task not needing to have a reference to the incident, Breeze does not seems to support that correctly, is there a way to configure that?


回答1:


I am not that familiar with the Breeze OData adapter and you are talking about unstable, unreleased technologies here when you refer to the "ASP.Net VNext version". You are working on the bleeding edge.

If you like it there, you are welcome to dive into the Breeze OData "dataserviceAdapter" (not documented but in GitHub) and the "jsonResultsAdapter". With the latter you should be able to bend whatever the server sends you into something useful on the client (if you can't, I want to know about it).




回答2:


As of version 1.4.4, available now, Breeze supports OData v3. This was likely the issue you were experiencing.

Previously only OData v2 was supported.



来源:https://stackoverflow.com/questions/18363153/breeze-working-with-web-api-odata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!