问题
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