public ActionResult Edit(int?id, int?opc) { if (opc == 1) { try { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } List <SelectListItem> genres = new List <SelectListItem>(); genres.Add(new SelectListItem() { Value = "M", Text = "Male", Selected = true }); genres.Add(new SelectListItem() { Value = "F", Text = "Female" }); ViewData["Genres"] = genres; PersonPhysicalPersonViewModel person = this.Get_Person_PhysicalPersonForID(id); if (person == null) { return(HttpNotFound()); } return(View(person)); } catch (Exception) { throw; } } else { try { PHYSICALPERSON pp = context.PHYSICALPERSON.Find(id); PERSON pes = context.PERSON.Find(id); context.PHYSICALPERSON.Remove(pp); context.PERSON.Remove(pes); context.SaveChanges(); return(RedirectToAction("Index")); } catch (Exception) { throw; } } }
public static int Edit_PhysicalPerson(int id, string name, string email, decimal salary, DateTime dateBirth, char genre) { dbRegistrationContext context1 = new dbRegistrationContext(); dbRegistrationContext context2 = new dbRegistrationContext(); try { using (TransactionScope scope = new TransactionScope()) { PERSON pes = new PERSON() { ID = id, NAME = name, EMAIL = email }; PHYSICALPERSON pp = new PHYSICALPERSON() { ID = pes.ID, PERSON_ID = pes.ID, SALARY = salary, DATEBIRTH = dateBirth, GENRE = genre.ToString() }; context1.PERSON.Attach(pes); context1.Entry(pes).State = EntityState.Modified; context2.PHYSICALPERSON.Attach(pp); context2.Entry(pp).State = EntityState.Modified; try { context2.SaveChanges(); context1.SaveChanges(); scope.Complete(); return(1); } catch (Exception) { return(-1); } } } catch (Exception) { return(-1); } }
public static int Delete_PhysicalPerson(int id) { dbRegistrationContext context1 = new dbRegistrationContext(); dbRegistrationContext context2 = new dbRegistrationContext(); try { using (TransactionScope scope = new TransactionScope()) { PERSON pes = new PERSON() { ID = id }; PHYSICALPERSON pp = new PHYSICALPERSON() { ID = pes.ID }; context2.PHYSICALPERSON.Attach(pp); context2.PHYSICALPERSON.Remove(pp); context1.PERSON.Attach(pes); context1.PERSON.Remove(pes); try { context2.SaveChanges(); context1.SaveChanges(); scope.Complete(); return(1); } catch (Exception) { return(-1); } } } catch (Exception) { return(-1); } }
public ActionResult Edit([Bind(Include = "Id,Name,Email,Salary,DateBirth,Genre")] PersonPhysicalPersonViewModel person) { try { if (ModelState.IsValid) { PERSON pes = new PERSON() { ID = person.Id, NAME = person.Name, EMAIL = person.Email }; PHYSICALPERSON pp = new PHYSICALPERSON() { ID = person.Id, PERSON_ID = person.Id, SALARY = person.Salary, DATEBIRTH = person.DateBirth, GENRE = person.Genre }; context.PERSON.Add(pes); context.Entry(pes).State = EntityState.Modified; context.PHYSICALPERSON.Add(pp); context.Entry(pp).State = EntityState.Modified; context.SaveChanges(); return(RedirectToAction("Index")); } return(View(person)); } catch (Exception) { throw; } }
public static int Insert_PhysicalPerson(string name, string email, decimal salary, DateTime dateBirth, char genre) { #region With explicit Transaction dbRegistrationContext context1 = new dbRegistrationContext(); dbRegistrationContext context2 = new dbRegistrationContext(); try { using (TransactionScope scope = new TransactionScope()) { int id = 0; try { id = context1.PERSON.Max(p => p.ID + 1); } catch (Exception) { id = 1; } PERSON pes = new PERSON() { ID = id, NAME = name, EMAIL = email }; context1.PERSON.Add(pes); PHYSICALPERSON pp = new PHYSICALPERSON() { ID = pes.ID, PERSON_ID = pes.ID, SALARY = salary, DATEBIRTH = dateBirth, GENRE = genre.ToString() }; context2.PHYSICALPERSON.Add(pp); try { context1.SaveChanges(); context2.SaveChanges(); scope.Complete(); return(1); } catch (Exception) { return(-1); } } } catch (Exception) { return(-1); } #endregion #region Without explicit Transaction /* * using (dbRegistrationContext context = new dbRegistrationContext()) * { * int id = context.PERSON.Max(p => p.ID + 1); * * PERSON pes = new PERSON() * { * ID = id, * NAME = name, * EMAIL = email * }; * * PHYSICALPERSON pp = new PHYSICALPERSON() * { * ID = pes.ID, * PERSON_ID = pes.ID, * SALARY = salary, * DATEBIRTH = dateBirth, * GENRE = genre.ToString() * }; * * context.PERSON.Add(pes); * context.PHYSICALPERSON.Add(pp); * context.SaveChanges(); * return 1; * } */ #endregion }