Web API 2 OData v4请求派生实体集合保留响应404

时间:2015-05-04 07:58:25

标签: asp.net-web-api2 odata-v4

我正在尝试本教程:Requesting a Derived Entity Collection

当我提出此请求时:

获取:http://tdd.stooges.com.my/api/paymentAbles?$format=application/json

我收到了这个回复:

{
  "@odata.context":"http://tdd.stooges.com.my/api/$metadata#paymentAbles",
  "value":[
    {
      "@odata.type":"#EFDB.Topup","id":1
    },
    {
      "@odata.type":"#EFDB.Order","id":7
    }
  ]
}

没关系。但是,当我尝试这个请求时:

获取:http://tdd.stooges.com.my/api/paymentAbles/EFDB.Order?$format=application/json

我收到回复404

我在stackoverflow上发现了一个类似的问题:

WCF Data Service gives 404 when making OData requests for derived types

但解决方案使所有http请求返回500内部错误。

我该如何解决这个问题?您可以使用此站点:http://tdd.stooges.com.my进行测试(例如使用firebug查看请求/响应详细信息)。

更新:

    modelBuilder.Entity<PaymentAble>()
       .Map<Topup>(s => s.Requires("type").HasValue("topup"))
       .Map<Order>(m => m.Requires("type").HasValue("order"));

[Table("payment_able")]   
public abstract class PaymentAble
{
    [Key]       
    public int id { get; set; }      
    public double amount { get; set; }
    public string code { get; set; }
    public string statusEnum { get; set; }
    [ForeignKey("member")]
    public int member_id { get; set; }

    public virtual Member member { get; set; }
    public virtual Payment payment { get; set; } 

    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public DateTimeOffset rowCreatedDT { get; set; }
    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    [ConcurrencyCheck]
    public byte[] rowVersion { get; set; }
    [ForeignKey("rowCreator")]
    public int rowCreatorLoginPerson_id { get; set; }
    [ForeignKey("rowLastModifiedBy")]
    public int rowLastModifiedByLoginPerson_id { get; set; }

    public virtual LoginPerson rowCreator { get; set; }
    public virtual LoginPerson rowLastModifiedBy { get; set; }
}
public class Topup : PaymentAble
{

}
public class Order : PaymentAble
{
    [Column("order_GSTPercent")]
    public double GSTPercent { get; set; }
    [Column("order_clientName")]
    public string clientName { get; set; }
    [Column("order_clientEmail")]
    public string clientEmail { get; set; }
    [Column("order_clientHp")]
    public string clientHp { get; set; }

    public virtual List<OrderItem> items { get; set; }
}
[Table("order_item")]
public class OrderItem : RowInfo
{
    [Key]
    public int id { get; set; }
    public int qty { get; set; }
    public double amount { get; set; }
    [ForeignKey("order")]
    public int order_id { get; set; }

    public virtual Order order { get; set; }
    public virtual OrderCard card { get; set; }        
}
    public static IEdmModel GetModel()
    {
        ODataModelBuilder builder = new ODataConventionModelBuilder();
        builder.EntitySet<LoginPerson>("loginPersons");
        builder.EntitySet<Admin>("admins");
        builder.EntitySet<Member>("members");
        builder.EntitySet<Card>("cards");
        builder.EntitySet<CardPackage>("cardPackages");
        builder.EntitySet<Game>("games");          
        builder.EntitySet<Img>("imgs");
        builder.EntitySet<Currency>("currencys");
        builder.EntitySet<Topup>("topups");
        builder.EntitySet<Order>("orders");
        builder.EntitySet<OrderItem>("OrderItems");
        builder.EntitySet<Payment>("payments");
        builder.EntitySet<PaymentAble>("paymentAbles");

        builder.Namespace = "RPC"; //test only
        var gettotalFn = builder.EntityType<PaymentAble>().Collection.Function("getTotal");
        gettotalFn.Returns<int>();  

        return builder.GetEdmModel();
    }

config.MapODataServiceRoute("odata", "api", GetModel());

[ODataRoutePrefix("paymentAbles")]
public class PaymentAblesController : BaseController
{
    [ODataRoute("")]
    [EnableQuery(AllowedQueryOptions = AllowedQueryOptions.All)]
    public IQueryable<PaymentAble> get()
    {
        return db.paymentAbles;
    }     

    public async Task<IHttpActionResult> getTotal()  
    {
        return Ok(15);
    }
}

0 个答案:

没有答案