发布到Odata服务仅接收pimitive数据

时间:2015-02-26 17:03:53

标签: c# entity-framework-4 odata

我正在建立一个Odata网络服务并利用实体框架,我的目的是发布一个具有一些原始属性的给定收据。收据还包含一组ReceiptLines,它也由原始属性组成。

这是我的问题:我可以发布到服务就好了。当我使用调试器在服务器端单步执行Post过程时,我发布的Receipt不再包含任何ReceiptLines。我确认我确实提交了ReceiptLinipt的收据。我在设计中遗漏了哪些内容从客户端发布时省略了ReceiptLines?

我已经从我的ReceiptController,WebApiConfig和下面的元数据中包含了我的代码。任何建议都是最受欢迎的。

ReceiptController:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.OData;
using System.Web.OData.Routing;
using AccountsReceivableWebService.Models;

namespace AccountsReceivableWebService
{
  public class ReceiptsController : ODataController
  {
    static ReceiptsContext db = new ReceiptsContext() { Receipts = new List<Receipt>() };

    #region Receipt portion

    private bool ReceiptExitsts(string key)
    {
      return db.Receipts.Count(r => r.ReceiptId == key) > 0;
    }

    [EnableQuery]
    public IQueryable<Receipt> Get()
    {
      return db.Receipts.AsQueryable();
    }

    [EnableQuery]
    public Receipt Get([FromODataUri] int key)
    {
      return db.Receipts.First(r=>r.ReceiptId==key.ToString());
    }

    /// <summary>
    /// This is the Post portion of CRUD operations.
    /// </summary>
    /// <param name="receipt"></param>
    /// <returns></returns>
    public IHttpActionResult Post(Receipt receipt)
    {
      if (!ModelState.IsValid) return BadRequest(ModelState);
      if (!receipt.Insert())
      {
        //db.Receipts.Add(receipt);
        BadRequest(receipt.Proof());
      }
      else
      {
        if (db.Receipts.Count(r => r.ReceiptId == receipt.ReceiptId) == 0)
          db.Receipts.Add(receipt);
      }
      return Created(receipt);
    }

    /// <summary>
    /// Checks that this receipt is a valid receipt
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    public IHttpActionResult Proof([FromBody] Receipt parameters)
    {
      string returnStr = "";
      if (!ModelState.IsValid) return BadRequest(ModelState);
      foreach (Receipt r in db.Receipts)
      {
        string temp = r.Proof();
        if (temp != "")
        {
          returnStr = string.Format("{0}: {1};", r.ReceiptId, temp);
        }
      }
      return Ok(new List<string>() {"hello"});
    }
    #endregion

    #region ReceiptLine portion

    [ODataRoute("Receipts({receiptId})/ReceiptLines/AccountsReceivableWebService.GetCount()")]
    public IHttpActionResult GetCount(string receiptId)
    {
      return Ok(db.Receipts.Single(r => r.ReceiptId == receiptId).ReceiptLines.Count);
    }
    #endregion
  }
}

WebApiConfig:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using AccountsReceivableWebService.Models;
using System.Web.OData.Builder;
using System.Web.OData.Extensions;

namespace AccountsReceivableWebService
{
  public static class WebApiConfig
  {
    public static void Register(HttpConfiguration config)
    {
      // Web API configuration and services

      // Web API routes
      ODataConventionModelBuilder builder = new ODataConventionModelBuilder();

      builder.Namespace = "AccountsReceivableWebService";
      builder.EntitySet<Receipt>("Receipts");
      builder.EntityType<ReceiptLine>().Collection.Function("GetCount").Returns<int>();

      config.MapODataServiceRoute(
            routeName: "ODataRoute",
            routePrefix: null,
            model: builder.GetEdmModel());

    }
  }
}

的元数据:

<edmx:Edmx xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx" Version="4.0">
  <edmx:DataServices>
    <Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="AccountsReceivableWebService.Models">
      <EntityType Name="Receipt">
        <Key>
          <PropertyRef Name="ReceiptId"/>
        </Key>
        <Property Name="ReceiptId" Type="Edm.String" Nullable="false"/>
        <Property Name="comments" Type="Edm.String"/>
        <Property Name="remitter" Type="Edm.String"/>
        <Property Name="ReceiptLineId" Type="Edm.String"/>
        <NavigationProperty Name="ReceiptLines" Type="Collection(AccountsReceivableWebService.Models.ReceiptLine)" ContainsTarget="true"/>
      </EntityType>
      <EntityType Name="ReceiptLine">
        <Key>
          <PropertyRef Name="ReceiptLineId"/>
        </Key>
        <Property Name="ReceiptLineId" Type="Edm.String" Nullable="false"/>
        <Property Name="Comments" Type="Edm.String"/>
        <Property Name="FiscalYear" Type="Edm.String"/>
        <Property Name="CostCenter" Type="Edm.String"/>
        <Property Name="object" Type="Edm.String"/>
        <Property Name="Amount" Type="Edm.String"/>
        <Property Name="Function" Type="Edm.String"/>
      </EntityType>
    </Schema>
    <Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="AccountsReceivableWebService">
      <Function Name="GetCount" IsBound="true">
      <Parameter Name="bindingParameter" Type="Collection(AccountsReceivableWebService.Models.ReceiptLine)"/>
      <ReturnType Type="Edm.Int32" Nullable="false"/>
      </Function>
      <EntityContainer Name="Container">
      <EntitySet Name="Receipts" EntityType="AccountsReceivableWebService.Models.Receipt"/>
      </EntityContainer>
    </Schema>
  </edmx:DataServices>
</edmx:Edmx>

0 个答案:

没有答案