Hi,
I'm trying to update 2 tables at the same time using only one event. Below is code, however, I can't make it work in second update described below.
The error is at the bottom of this page
Does anyone has any idea what's going on?
thanks in advance.
This is the code:
- [HttpPost]
- [ValidateAntiForgeryToken]
- public async Task
Edit( int id, [Bind("Companyid,Name,Code,Currency")] Company company) - {
- if (id != company.Companyid)
- {
- return NotFound();
- }
- if (ModelState.IsValid)
- {
- try
- {
- TempData["Confirmation"] = company.Name + " was updated.";
- _context.Update(company);
- await _context.SaveChangesAsync();
- //Until here, the code above works fine and no errors.
- // this is the code that it insn't working. I have a second table 'CostCenter' which Key is CCid
- // this is a simple update method I'm trying to implement.
- int ccid = 1;
- CostCenter costCenter = new CostCenter();
- costCenter = _context.Costcenter.Find(ccid);
- costCenter.Department = "Testing....";
- _context.SaveChanges();
- }
- catch (DbUpdateConcurrencyException)
- {
- if (!CompanyExists(company.Companyid))
- {
- return NotFound();
- }
- else
- {
- throw;
- }
- }
- return RedirectToAction(nameof(Index));
- }
- return View(company);
- }

Rijwan AnsariPosted Feb 16, 2021, 4:29 PM
Paulo Luis ZampieriPosted Feb 16, 2021, 5:40 PM
Paulo Luis ZampieriPosted Feb 16, 2021, 4:47 PM