Home > Net >  Adding OrderDetail to database using Entity Framework
Adding OrderDetail to database using Entity Framework

Time:10-27

I'm making Coffee Shop app using .NET Core.

I have OrderHeader and OrderDetail entities. I created model classes for OrderHeader and OrderDetail and connect them using a foreign key. In the controller, when I try to save data to OrderHeader, for the OrderDetail noting happens.

This is my code:

[HttpPost]
[ValidateAntiForgeryToken]
[ActionName("Summary")]
public IActionResult SummaryPost(ProductUserVM ProductUserVM)
{
    var claimsIdentity = (ClaimsIdentity)User.Identity;
    var claim = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier);

    OrderHeader orderHeader = new OrderHeader()
        {
            ApplicationUserId = claim.Value,
            FullName = ProductUserVM.ApplicationUser.FullName,
            OrderDate = DateTime.Now
        };

    _db.OrderHeader.Add(orderHeader);
    _db.SaveChanges();

    foreach(var prod in ProductUserVM.ProductList)
    {
        OrderDetail orderDetail = new OrderDetail()
            {
                OrderHeaderId = orderHeader.Id,
                ProductId = prod.Id
            };
        _db.OrderDetail.Add(orderDetail);
    }

    _db.SaveChanges();

    return RedirectToAction(nameof(Confirmation));
}

Can someone explain what is going on?

CodePudding user response:

Instead of referring to the Id, you want to reference the whole object.

Add the OrderDetail as a collection to the OrderHeader.

public class OrderHeader {

  // other properties

  public virtual IColletion<OrderDetail> OrderDetail { get; set; }

}

And add the OrderHeader to the OrderDetail:

public class OrderDetail {

  // other properties

  public virtual OrderHeader OrderHeader { get; set; }

}

If you've done this already, continue below.

Now instead of creating a new OrderDetail with the OrderHeaderId, add a reference to OrderHeader instead.

foreach (var prod in ProductUserVM.ProductList)
{
    OrderDetail orderDetail = new OrderDetail()
    {
        OrderHeader = orderHeader,
        ProductId = prod.Id
    };

    _db.OrderDetail.Add(orderDetail);
}

Documentation on relationships

CodePudding user response:

Yes, but see my models :

OrderHeader model

public class OrderHeader
    {
        [Key]
        public int Id { get; set; }

        public string ApplicationUserId { get; set; }

        [ForeignKey("ApplicationUserId")]
        public ApplicationUser ApplicationUser { get; set; }

        public DateTime OrderDate { get; set; }

        [Required]
        public string FullName { get; set; }

    }

and OrderDetail model:

public class OrderDetail
    {
        [Key]
        public int Id { get; set; }


        public int OrderHeaderId { get; set; }

        [ForeignKey("OrderHeaderId")]
        public virtual OrderHeader OrderHeader { get; set; }


        public int ProductId { get; set; }

        [ForeignKey("ProductId")]
        public virtual Product Product { get; set; }
    }
  • Related