0

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

checkout.ts

  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 this.http.post<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

[HttpPost]
public async Task<ActionResult<Orders>> PostOrders(Orders orders)
{
  _context.Orders.Add(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; }
}

1 Answer 1

0

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!

Sign up to request clarification or add additional context in comments.

1 Comment

OrderId is a primary key in Order Table and OrderId in OrderItems is a foreign key, I know when I add an entry it will return the entry that is added, But what I want is to send both order and orderitems table values and it should auto setup primary key in order table and same key value as foreign key in orderitems table.

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.