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; }
}
prodproperties? you haveOrderProduct prod = new OrderProduct();but nothing likeprod.OrderID = ??; prod.Product="??