我是Web API的新手。 我正在为Order创建Web API POST方法。我创建了两个表 1)Tbl_Order有列 - Order_ID -Primary Key,Employee_ID,TotalBill,
2)Tbl_OrderMenu有列 - OrderMenuID -Primary Key,Order_ID -Foreign Key from Tbl_Order,MenuName,Quantity
表之间的关系是一对多,即Tbl_Order(1)-to-Tbl_OrderMenu(*或Many)。
如何创建用于POST方法的Web API以同时创建数据?
public class myCombinedClass
{
public int Order_ID {get;set;}
public int Employee_ID {get;set;}
public string TotalBill {get;set;}
public List<Tbl_OrderMenu> OrderMenu { get; set; }
}
我的API POST控制器是
public HttpResponseMessage Post(myCombinedClass myData)
{
if(myData.id == 0)
{
// This is a new entry to Tbl_Order.
}
else
{
//You just need to update the Tbl_Order. I mean, here you are adding
//data to Tbl_OrderMenu table with Order_ID.
}
}
POST方法会有什么结果。
答案 0 :(得分:0)
@manish,这是使用EF的POST方法的一个例子:
// POST: api/myCombinedClasses
[ResponseType(typeof(myCombinedClass))]
public async Task<IHttpActionResult> PostmyCombinedClass(myCombinedClass mycombinedclass)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.myCombinedClasses.Add(mycombinedclass);
db.OrderMenus.AddRange(mycombinedclass.OrderMenu);
await db.SaveChangesAsync();
return CreatedAtRoute("DefaultApi", new { id = mycombinedclass.Order_ID }, mycombinedclass);
}