0

I am creating web API for Order. in which two tables I have created

1)Tbl_Order have columns-

Order_ID     -Primary Key,
Employee_ID,
TotalBill,

2)Tbl_OrderMenu have columns-

OrderMenuID    -Primary Key,
Order_ID       -Foreign Key from Tbl_Order,
MenuName,
Quantity

the relationship between tables is one-to-many i.e. Tbl_Order(1)-to-Tbl_OrderMenu(* or Many).

How can I create Web API to POST method to create Data at the same time?

Basanta Matia
  • 1,504
  • 3
  • 15
  • 27
manish jadhav
  • 15
  • 2
  • 10

1 Answers1

0

See, in this case, first you need to add data to Tbl_Order table. Then you can add as many data to Tbl_OrderMenu table, by taking Order_ID from the first table. Hope you understood this.

Create a class which should include both the table. For example,

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; }
}

In your API Controller method, you can write like this,

[HttpPost]
public HttpResponseMessage Post(myCombinedClass myData)
{
 if(myData.id == 0)
  {
    // This is a new entry to Tbl_Order.
    Tbl_Order orderObject = new Tbl_Order();
    orderObject.Employee_ID = myData.Employee_ID;
    orderObject.TotalBill = myData.TotalBill;
    _ctx.Tbl_Order.Add(orderObject);
    _ctx.SaveChanges();
  }
 else
  {
   //You just need to update the Tbl_Order. I mean, here you are adding 
   //data to Tbl_OrderMenu table with  Order_ID.
   var OrderMenuData = _ctx.Tbl_OrderMenu.where(m=>m.Order_ID == 
                       myData.id).FirstOrDefault(); 
   OrderMenuData.Order_ID = myData.id;     
   OrderMenuData.MenuName = myData.OrderMenu.MenuName;
   OrderMenuData.Quantity = myData.OrderMenu.Quantity;
   _ctx.Entry(OrderMenuData).State=System.Data.Entity.EntityState.Modified;
   _ctx.SaveChanges();
  }
}

Note: See here, _ctx is the context class, that you need to add in this controller/method.

Ex: MyDBContext _ctx = new MyDBContext();

I have written an answer same like this here. Go through that. Check how I'm passing data to both the table in ajax method.

Community
  • 1
  • 1
Basanta Matia
  • 1,504
  • 3
  • 15
  • 27
  • actually, I am new to Web API.can you please elaborate what will come in Post Method. – manish jadhav Mar 20 '17 at 13:25
  • thank you @BasantaMatia. it works for me for 50%. but I want to add data in **Tbl_OrderMenu** on behalf on **OrderID**. I mean Tbl_OrderMenu should contain Data respect to OrderID. – manish jadhav Mar 20 '17 at 14:13
  • thank you @BasantaMatia . it works for me for 50%. but I want to add data in **Tbl_OrderMenu** on behalf on **OrderID**. I mean Tbl_OrderMenu should contain Data respect to OrderID. Consider data in **Tbl_Order** is : **(OrderID:1,EmpolyeeID:101, TotalBill:10)** then data in **Tbl_OrderMenu** shhould like **(OrderMenuId:1,OrderID:1,Menu:Bread)** should be added – manish jadhav Mar 20 '17 at 14:19
  • That is what I did in my answer. U just need to understand the code properly. U need to pass data for Tbl_OrderMenu table including OrderID. Check where u are getting error and try to debug. If it solve ur problem plz don't forget to mark as answer. – Basanta Matia Mar 20 '17 at 14:25
  • sir i do as per your instructions but it not working as per my requirement. by using your code i can only create record in **Tbl_Order** table.there is no record in **Tbl_Order,menu** . please help me. – manish jadhav Mar 20 '17 at 16:39
  • I have attached a link in my answer, did u check that 1 ? It's d complete answer. Bdw can u tell me from where u are calling the API method? How u are calling this API method, can u post the code here. – Basanta Matia Mar 20 '17 at 16:46
  • i post code as { "Tbl_OrderMenu":[{ "MenuID": 1, "MenuName": "Idli", }], "NumberOfItems": 1, "TotalBill": 20.0, } – manish jadhav Mar 20 '17 at 16:49
  • That's fine. Please chick on d link I have mentioned in my answer. U can see how the data u need to pass. The way u r passing data, it's wrong. And just put a break point in ur API method and check. Plz check that link. – Basanta Matia Mar 20 '17 at 16:55
  • sir I visit your link but I can't do what I want. – manish jadhav Mar 20 '17 at 17:09
  • your code generates record in **Tbl_Order**.please tell me how to add record in **Tbl_OrderMenu** what i want to do. – manish jadhav Mar 20 '17 at 17:11
  • If u have written the API method exactly the way I mentioned in my answer, then post ur json data like this [{Order_ID:1, Tbl_OrderMenu: [{ Order_ID:1,MenuName:"Manish",Quantity:1}]}] If OrderMenuID is auto generated then no need else include that one too inside Tbl_OrderMenu:[{}] Put a breakpoint n check in ur API method. – Basanta Matia Mar 20 '17 at 17:24
  • sir Order_ID is also autogenerated. System must able to set OrderID for records in Tbl_OrderMenu table – manish jadhav Mar 20 '17 at 17:27
  • Order_ID will b auto generated for Tbl_Order table, not for Tbl_OrderMenu table. So u need to pass it the way I mentioned in my previous comment. The orderId should b present in db before u saving data for 2nd table. – Basanta Matia Mar 20 '17 at 17:31
  • ok sir, Finally got what i want. Thank you so much. i do mistakes while post data. – manish jadhav Mar 20 '17 at 17:35