Hi,
I have two tables. Order and Item.
I am creating MVC application using repository design. For insertion both these tables receive data and this is working.
however the Item_id is not appering in the Order table.
public class Order
{
[Key]
public int Order_id { get; set; }
public List<Item> Order_list { get; set; }
public int Order_num { get; set; }
public string Supplier_contact_person { get; set; }
...
}
public class Item
{
[Key]
public int Item_id { get; set; }
public int Order_id { get; set; }
public Order Order { get; set; }
public string Item_name { get; set; }
public string Description { get; set; }
...
}
// POST: OrderController/Create
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create(InsertOrderVM model)
{
try
{
var orderModel = new InsertOrderVM
{
//order table
Supplier_contact_person = model.Supplier_contact_person,
Branch = model.Branch,
Requested_by = model.Requested_by,
Delivery_address_line1 = model.Delivery_address_line1,
Delivery_address_line2 = model.Delivery_address_line2,
Delivery_address_county = model.Delivery_address_county,
Delivery_address_postcode = model.Delivery_address_postcode,
Order_date = model.Order_date,
Delivery_date = model.Delivery_date,
Payment_terms = model.Payment_terms,
Requested = true
};
var requestOrder = _mapper.Map<Order>(orderModel);
await _unitOfWork.Orders.Create(requestOrder);
await _unitOfWork.Save();
var itemModel = new InsertOrderVM
{
//item table
Item_name = model.Item_name,
Description = model.Description,
Department = model.Department,
Quantity = model.Quantity,
Vat_rate = model.Vat_rate,
Net_unit_price = model.Net_unit_price,
Total_net = model.Total_net,
Total = model.Total
};
var requestItem = _mapper.Map<Item>(itemModel);
await _unitOfWork.Items.Create(requestItem);
await _unitOfWork.Save();
return RedirectToAction("Index");
}
catch (Exception ex)
{
ModelState.AddModelError("", "Something went wrong, please try again.");
return View("Index");
}
}
The data reaches both tables but the Item_id does not appear in the Order table, it just says null.
It needs to show the item_id of the newly added record.
Please help