如何使用WCF和实体框架获取嵌套的JSON Reply

时间:2013-02-14 07:31:32

标签: json entity-framework

我有2个模型,A Order Header和Order Detail。

我想最终得到服务器的JSON回复,如下所示:

--OrderHeader 1
  |
   --OrderDetails 1
--OrderHeader 2
  |
   --Order Detail 2

我会使用实体框架创建某种子查询wand通过我的WebGet返回这个,还是我会运行单独的查询并在获得结果后整合格式?

以下是我的Viewmodels:

 public class OpenOrderHeader
{
    public int CustomerID { get; set; }
    public int OrderID { get; set; }
    public int OrderUniqueNumber { get; set; }
    public int NumberOfProductsOnOrder { get; set; }
    public DateTime OrderDateCreated { get; set; }
    public DateTime OrderDateUpdated { get; set; }
    public string OrderLocalOnline { get; set; }
    public int BranchID { get; set; }
    public string PaymentMethod { get; set; }
    public int OrderStatus { get; set; }
    public string OrderCurrency { get; set; }
    public decimal OrderConversionRate { get; set; }
    public decimal SubTotalInclTax { get; set; }
    public decimal SubTotalExclTax { get; set; }
    public decimal DiscountInclTax { get; set; }
    public decimal DiscountExclTax { get; set; }
    public decimal ShippingInclTax { get; set; }
    public decimal ShippingExclTax { get; set; }
    public decimal PaymentFeeInclTax { get; set; }
    public decimal PaymentFeeExclTax { get; set; }

}

public class OpenOrderProducts
{
    public int OrderID { get; set; }
    public string ProductSKUName { get; set; }
    public int ProductSKUID { get; set; }
    public string ProductSKUStockCode { get; set; }
    public DateTime ProductAddedToOrder { get; set; }
    public int QtyOfProductsOnOrder { get; set; }
    public decimal UnitPriceinclTax { get; set; }
    public decimal UnitPriceExclTax { get; set; }
    public decimal UnitDiscountInclTax { get; set; }
    public decimal UnitDiscountExclTax { get; set; }
    public decimal LineItemTotalIncludingTax { get; set; }
    public decimal LineItemExclTax { get; set; }
    public decimal LineItemShippingCost { get; set; }
}

1 个答案:

答案 0 :(得分:1)

只需在Orderdetails实体上创建导航属性OrderHeader即可。然后你可以查询

var data = db.OrderHeaders.Include(h => h.Orderdetails).ToList();

将数据序列化为JSON。

我认为您在展示OpenOrderHeader的课程中会有OpenOrderProducts的集合?

public virtual ICollection<OpenOrderProduct> OpenOrderProducts { get; set; }

(如您所见,我会更改OpenOrderProduct中的班级名称,不会复数)