Issue with inheritance with WebAPI, OData v3 and BreezeJs

六眼飞鱼酱① 提交于 2019-12-08 02:45:39

问题


We are using WebAPI 2.2, with OData v3 and BreezeJS and are having an issue when using inheritance, we have a setup along the following lines (simplified obviously for this issue)

We have a Vehicle abstract class and then two other classes (Bus and Car) which inherit from Vehicle, such as:

public abstract class Vehicle 
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class Bus : Vehicle
{
    public int NumberOfSeats { get; set; }
}

public class Car : Vehicle
{
     public string Colour { get; set; }
}

We then have an Activity class which can have a single Vehicle (either a Car or a Bus):

public class Activity
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public DateTime DueDate { get; set; }
    public int VehicleId { get; set; }
    public virtual Vehicle Vehicle { get; set; }
}

We would like to be able to query the Activity and expand its Vehicle and get the relevent Car or Bus back, such as https://dummysitename/api/Activities?$expand=Vehicle, which is working fine. We would also like to be able to GET/PATCH/POST to endpoints for Cars and Buses (such as https://dummysitename/api/Cars and https://dummysitename/api/Buses), however we get a 404 when trying to do this.

Our metadata is created by the breeze EdmBuilder. We have tested it not using the EdmBuilder but using the ODataConventionModelBuilder and that works fine for these scenarios, but doesn't obviously work for us in the grander scheme of things as we will be utilizing breeze.js heavily.

Any ideas on why we can't use the Cars and Buses endpoints when using the EdmBuilder would be greatly appreciated.

UPDATE: It would appear that the issue is being caused by the Vehicle being stipulated on the Activity class. With the Vehicle on the Activity class the EntityContainer section of the metadata looks like this:

        <EntityContainer Name="TodoListContext" p5:UseClrTypes="true" xmlns:p5="http://schemas.microsoft.com/ado/2013/11/edm/customannotation">
            <EntitySet Name="Activities" EntityType="ODataBreezejsSample.Models.Activity" />
            <EntitySet Name="Vehicles" EntityType="ODataBreezejsSample.Models.Vehicle" />
            <AssociationSet Name="Activity_Vehicle" Association="ODataBreezejsSample.Models.Activity_Vehicle">
                <End Role="Activity_Vehicle_Source" EntitySet="Activities" />
                <End Role="Activity_Vehicle_Target" EntitySet="Vehicles" />
            </AssociationSet>
        </EntityContainer>

however if the Vehicle is removed from the Activity class then that same section looks like this:

        <EntityContainer Name="TodoListContext" p5:UseClrTypes="true" xmlns:p5="http://schemas.microsoft.com/ado/2013/11/edm/customannotation">
            <EntitySet Name="Activities" EntityType="ODataBreezejsSample.Models.Activity" />
            <EntitySet Name="Buses" EntityType="ODataBreezejsSample.Models.Bus" />
            <EntitySet Name="Cars" EntityType="ODataBreezejsSample.Models.Car" />
        </EntityContainer>

at which point the Bus and Car endpoints become available, however this is not really an option as we require the Activity to contain the base class Vehicle.

来源:https://stackoverflow.com/questions/32250002/issue-with-inheritance-with-webapi-odata-v3-and-breezejs

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