I'm implementing asp.net core project. In my Controller class, in its Edit method, I wrote some code like below:
In my code I have 3 tables Applicant and PersonApplicant and LegalApplicant. Applicant has one to one relationship with LegalApplicant and PersonApplicant. Eachtime Applicant refers to one of them. They are connected with each other as the primary key {ApplicantID,ApplicantType} and if ApplicantType is 1, Applicant refers to PersonApplicant and if it is 2, Applicant refers to LegalApplicant. In the view, the user should choose from a selectlist about the type of applicant he wants to update. Now my problem is in updating those tables when user wants to change the ApplicantType. For instance, if the user change ApplicantType from LegalApplicant to PersonApplicant then the former record in Legal applicant should be deleted according what I did in the code and a new record should be inserted into PersonApplicant. For doing that I wrote like the above code, But after running the project, it shows me an error which is about not to accept the former applicant record exist and just refers to another table. In my methodology I can not delet the related record in Applicant and again insert a new one ther cause I need the former info about that record. I appreciate if anyone guide me how can I fix this.
public async Task<IActionResult> Edit(int id, ApplicantViewModel applicant) { var formerApplicantType = await _context.Applicant .Include(a => a.ApplicantTypeNavigation) .FirstOrDefaultAsync(m => m.ApplicantId == id); if (id != applicant.applicantvm.ApplicantId) { return NotFound(); } if (ModelState.IsValid) { try { if (applicant.applicantvm.ApplicantType == 1) { var UpdQuery = from a in _context.Applicant join p in _context.PersonApplicant on a.ApplicantId equals p.ApplicantId where applicant.applicantvm.ApplicantId == a.ApplicantId && applicant.applicantvm.ApplicantType == a.ApplicantType select new { a, p }; if (formerApplicantType.ApplicantType == 2) { Debug.WriteLine("opposite applicantType"); var legalApplicantForDelete = await _context.LegalApplicant.FindAsync(id); _context.LegalApplicant.Remove(legalApplicantForDelete); //????? var pa = new PersonApplicant() { BirthCertificateNo = applicant.personapplicantvm.BirthCertificateNo, IssuePlace = applicant.personapplicantvm.IssuePlace, NationalCode = applicant.personapplicantvm.NationalCode, Username = applicant.personapplicantvm.Username, Applicant = applicant.applicantvm }; using (var context = new CSSDDashboardContext()) { context.PersonApplicant.Add(pa); context.SaveChanges(); } } else { //---------------------------------------------- foreach (var x in UpdQuery.ToList()) { x.a.ApplicantType = applicant.applicantvm.ApplicantType; x.a.Address = applicant.applicantvm.Address; x.a.Description = applicant.applicantvm.Description; x.a.Name = applicant.applicantvm.Name; x.p.BirthCertificateNo = applicant.personapplicantvm.BirthCertificateNo; x.p.NationalCode = applicant.personapplicantvm.NationalCode; x.p.IssuePlace = applicant.personapplicantvm.IssuePlace; x.p.Username = applicant.personapplicantvm.Username; } } await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ApplicantExists(applicant.applicantvm.ApplicantId)) { return NotFound(); } else { throw; } } return RedirectToAction(nameof(Index)); } ViewData["ApplicantType"] = new SelectList(_context.EntityType, "Id", "Id", applicant.applicantvm.ApplicantType); return View(applicant); }
In my code I have 3 tables Applicant and PersonApplicant and LegalApplicant. Applicant has one to one relationship with LegalApplicant and PersonApplicant. Eachtime Applicant refers to one of them. They are connected with each other as the primary key {ApplicantID,ApplicantType} and if ApplicantType is 1, Applicant refers to PersonApplicant and if it is 2, Applicant refers to LegalApplicant. In the view, the user should choose from a selectlist about the type of applicant he wants to update. Now my problem is in updating those tables when user wants to change the ApplicantType. For instance, if the user change ApplicantType from LegalApplicant to PersonApplicant then the former record in Legal applicant should be deleted according what I did in the code and a new record should be inserted into PersonApplicant. For doing that I wrote like the above code, But after running the project, it shows me an error which is about not to accept the former applicant record exist and just refers to another table. In my methodology I can not delet the related record in Applicant and again insert a new one ther cause I need the former info about that record. I appreciate if anyone guide me how can I fix this.