0

I tried the below code to insert data to two tables but it inserts only for one table and the second table's data is not inserting it's just inserting the Tstamp column only. I tried to POST data from POSTMON.

 [HttpPost]
    public async Task<IHttpActionResult> PostOrder(CustomerOrder customerOrder)
    {
        using (GeoContext context = new GeoContext())
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var order = context.CustomerOrders.Add(customerOrder);
            context.CustomerOrders.Add(order);
            order.Tstamp = DateTime.Now;

            OrderProduct prod = new OrderProduct();

            var product = context.OrderProducts.Add(prod);
            context.OrderProducts.Add(product);
            order.OrderID = product.OrderID;
            product.Tstamp = DateTime.Now;

            await context.SaveChangesAsync();
        }

        return Ok(new { Message = "Order placed" });
    }

Below JSON values that i tried to insert from POSTMAN

{
"CustomerID":2,
"OrderNo":4,
"TotalPrice":50.00,
"Tstamp":"2021-03-31",
"Product":"Apple",
"Price":5,
"Quantity":10
}

CustomerOrder Class

 public partial class CustomerOrder
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public CustomerOrder()
    {
        this.OrderProducts = new HashSet<OrderProduct>();
    }

    public int OrderID { get; set; }
    public int CustomerID { get; set; }
    public Nullable<int> OrderNo { get; set; }
    public Nullable<double> TotalPrice { get; set; }
    public Nullable<System.DateTime> Tstamp { get; set; }

    public virtual Customer Customer { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<OrderProduct> OrderProducts { get; set; }
}

OrderProduct Class

 public partial class OrderProduct
{
    public int OrderProductID { get; set; }
    public int OrderID { get; set; }
    public string Product { get; set; }
    public Nullable<double> Price { get; set; }
    public Nullable<int> Quantity { get; set; }
    public Nullable<System.DateTime> Tstamp { get; set; }

    public virtual CustomerOrder CustomerOrder { get; set; }
}
5
  • Can you show CustomerOrder and OrderProduct classes pls? Commented Mar 31, 2021 at 8:59
  • @Sergey Edit the question. you can check now Commented Mar 31, 2021 at 12:42
  • You never do set the prod properties? you have OrderProduct prod = new OrderProduct(); but nothing like prod.OrderID = ??; prod.Product="?? Commented Mar 31, 2021 at 13:02
  • @RuiCaramalho How to pass data from two tables from POSTMON? Commented Mar 31, 2021 at 13:31
  • @Sergey gave you the answer :) Commented Mar 31, 2021 at 15:27

1 Answer 1

1

You have to create and fill an OrderProduct instance and add it to CustomerOrder in order to add a record to OrderProduct table.

{
    "customerID": 2,
    "orderNo": 4,
    "tstamp": "2021-03-31",
    "orderProducts": [
        {
        "Product":"Apples",
        "Price":5,
        "Quantity":10 
        }
    ]
}

Fix the action for a Postman:

   [HttpPost]
    public async Task<IHttpActionResult> PostOrder(CustomerOrder customerOrder)
    {
        using (GeoContext context = new GeoContext())
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

          double? orderSum = 0.0;
            foreach (var product in customerOrder.OrderProducts)
            {
                product.Tstamp = DateTime.Now;
                orderSum += ( (double)product.Price) * product.Quantity;
            }
             context.CustomerOrders.Add(customerOrder);
            await context.SaveChangesAsync();
        }

        return Ok(new { Message = "Order placed" });
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.