How to send post request for related data from angular to .NET core api

angular entity-framework-core


I'm new to angular and .NET core, I'm trying to store related data to sqlserver via .net core api. My problem is Order controller is receiving one Order object how It will store orderitems with same OrderId which will be generated in Order Table? Plus I'm unable to call order controller, my service method returning this error

Failed to load resource: the server responded with a status of 500 () POST https://localhost:44378/api/Orders 500


  placeorder() {

    this.postservice.placeorder(this.orderdata.value, this.cart.selections)

In this code orderdata.value are simple values while cart.selections are list of objects

Service code

  placeorder(orderdata: order, orderitems: orderItems): Observable<any[]>{
return<any[]>(this.myAppUrl + 'api/Orders', [orderdata,orderitems]);}

order Interface

export interface order{
  orderid: number;
  uid: number;
  orderdate: Date;
  orderstatus: string;


OrderItems interface

export interface orderItems extends Array<orderItem> {

export interface orderItem {
  productId?: number;
  productName?: string;
  productPrice?: number;
  quantityValue?: number;

Order Controller

public async Task<ActionResult<Orders>> PostOrders(Orders orders)
  await _context.SaveChangesAsync();

   return CreatedAtAction("GetOrders", new { id = orders.OrderId }, orders);

Order Model Class

    public partial class Orders
    public Orders()
        OrderItems = new HashSet<OrderItems>();

    public int OrderId { get; set; }
    public int? Uid { get; set; }
    public DateTime? OrderDate { get; set; }
    public string OrderStatus { get; set; }

    public virtual ICollection<OrderItems> OrderItems { get; set; }

OrderItems Model Class

public partial class OrderItems
    public int ItemId { get; set; }
    public int? ProductId { get; set; }
    public string ProductName { get; set; }
    public int? ProductPrice { get; set; }
    public int? ProductQuantity { get; set; }
    public int? OrderId { get; set; }

    public virtual Orders Order { get; set; }
4/15/2020 6:20:32 AM

Popular Answer

If I understood your request correctly, you would like to add articles related to an order that does not yet exist. In technical terms, you do not have the primary order key to indicate it as a foreign key on the articles.

When you add an entry in your DbContext and make a backup of this DbContext, the unique identifier is valued and returned to you on the previously added object.

If you add an "Order" object in your DbContext you cannot indicate the unique identifier there because SQL will take care of it. If you make a DbContext.SaveChanges(), know that the unique identifier of the order will be valued on the instance of the "Order" object previously added to your DbContext.

You just have to add the articles in your database with the unique identifier of the order.

I remain available if you have any questions, see you soon!

4/14/2020 6:16:16 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow