I have a problem and unfortunately can not find the solution! This is my database:
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!!
CodePudding user response:
It looks like you are setting document.Total correctly, are you perhaps overriding the Total value when you call document.addPaymentInformation(paymentInformation) further down?
CodePudding user response:
I set an breakpoint and it has shown that the id matches
transfer without success