skip to Main Content

I have a problem and unfortunately can not find the solution!
This is my database:

enter image description here

and I would like to store all PaymentSum(double) [PaymentInformation] in the Total (double) [Document].

This is the SQL statement:

Select SUM(PaymentSum)
from PaymentInformations
where DocumentId = 1;

I have tried this, but without success:

// POST: api/PaymentInformations
    // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
    [HttpPost("new/{eId}")]
    public async Task<ActionResult<PaymentInformation>> PostpaymentInformations(long eId, PaymentInformation paymentInformation)
    {
        Document document = await _context.Documents.FindAsync(eId);


        document.Total = _context.PaymentInformations
                            .Where(b => b.Document.Id == eId)
                            .Sum(a => a.PaymentSum);


        //document.Total = _context.PaymentInformations.FromSqlRaw("Select SUM(PaymentSum) from PaymentInformations where DocumentId = {0}",eId).FirstOrDefault();
       
       

        foreach (var item in _context.PaymentInformations)
        {
            System.Console.WriteLine(item.PaymentSum);
            System.Console.WriteLine(item.DocumentId);
         
        }
       
        _context.PaymentInformations.Add(paymentInformation);

        document.addPaymentInformation(paymentInformation);

        await _context.SaveChangesAsync();

        return CreatedAtAction("GetpaymentInformations", new { id = paymentInformation.Id }, paymentInformation);
       
    }

I hope someone can help me.

Thank you!!

3

Answers


  1. Chosen as BEST ANSWER

    I set an breakpoint and it has shown that the id matches

    enter image description here

    transfer without success

    enter image description here


  2. It looks like you are setting document.Total correctly, are you perhaps overriding the Total value when you call document.addPaymentInformation(paymentInformation) further down?

    Login or Signup to reply.
  3. public void addPaymentInformation(PaymentInformation paymentInformation)
        {
            if (PaymentInformations == null)
            {
                PaymentInformations = new List<PaymentInformation>();
            }
            paymentInformation.Document = this;
            PaymentInformations.Add(paymentInformation);
    
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search