使用 Asp.Net WebApi + EF + Odata 深度插入数据

Deep Inserting data with Asp.Net WebApi + EF + Odata

我正在使用 Odata v4 WebApi 2.2、Entity Framework 6 和 Odata v4 代理客户端 (WPF) 在一个项目中。

考虑以下代码:

//Model Class
public class Order
{
    public int OrderId {get;set;} //Auto Generated Id
    public string OrderDescription {get;set;}

    Public virtual IEnumerable<OrderLine> OrderLines;
}

//Model Class
public class OrderLine
{
    public int OrderId {get;set;} //Auto Generated Id
    public int OrderLineId {get;set;} //Key of the parent entity
    public string PartDescription  {get;set;}

    Public virtual Order Order;
}


//Odata Proxy Client Code
public void insert()
{
    Order order new Order;
    order.OrderDescription = "Test Desc";

    order.Add( new OrderLine{PartDescription = P100}) //OrderId & OrderLineId is null
    order.Add( new OrderLine{PartDescription = P101})//OrderId & OrderLineId is null
    order.Add( new OrderLine{PartDescription = P101})//OrderId & OrderLineId is null

    //When save changes is called OrderId needs to be set to order lines
    Context.SaveChanges();
}

我需要使用来自客户端的订单行创建订单并发送回数据库以进行保存。问题是 类 Order & OrderLine 的密钥是在数据库中自动生成的。

我需要进行深插入,操作需要按以下顺序进行。

  1. 需要先插入订单。在插入订单行之前。
  2. 订单行OrderId需要设置
  3. 需要插入订单行。

EF或Odata V4是否支持深插入? 我怎样才能做到这一点?

现在不支持深度插入,但您可以使用 $ref,

WebAPI端有个例子:

https://github.com/xuzhg/WebApiSample/tree/eb795e26547555666410a79b88e3930d22479798/WebApiODataSample