public int addNewRecord(string fName, string lName, string address, string country, string phone, string email, int sId, EmployeePosition position) { using (TransactionScope transaction = new TransactionScope((TransactionScopeOption.Required))) { try { int newId = -1; using (ElectricCarEntities context = new ElectricCarEntities()) { try { int max; try { max = context.People.Max(emp => emp.Id); } catch { max = 0; } newId = max + 1; context.People.Add(new Employee() { Id = newId, fName = fName, lname = lName, address = address, country = country, phone = phone, email = email, // together and after employee creation add logInfo and include returned // person Id in there // LoginInfoes = DLogInfo.buildLogInfos(logInfos), pType = PType.Employee.ToString(), sId = sId, position = position.ToString(), }); context.SaveChanges(); } catch (Exception e) { throw new SystemException("Cannot add new Employee record " + " with an error " + e.Message); } } transaction.Complete(); return newId; } catch (TransactionAbortedException e) { throw new SystemException("Cannot finish transaction for adding an Employee " + " with an error " + e.Message); } } }
public void updateRecord(int id, string fName, string lName, string address, string country, string phone, string email, ICollection<MLogInfo> logInfos, int sId, EmployeePosition position) { using (TransactionScope transaction = new TransactionScope((TransactionScopeOption.Required))) { try { using (ElectricCarEntities context = new ElectricCarEntities()) { try { Employee emp = (Employee) context.People.Find(id); emp.fName = fName; emp.lname = lName; emp.address = address; emp.country = country; emp.phone = phone; emp.email = email; // to be or not to be, cos LoginInfoes is 'virtual' in Customer EF model emp.LoginInfoes = DLogInfo.buildLogInfos(logInfos, emp.LoginInfoes); foreach (LoginInfo logInfo in emp.LoginInfoes) { logInfo.pId = emp.Id; logInfo.Person = emp; } emp.position = position.ToString(); emp.sId = sId; context.SaveChanges(); } catch (Exception e) { throw new SystemException("Cannot update Employee record " + id + " with message " + e.Message); } } transaction.Complete(); } catch (TransactionAbortedException e) { throw new SystemException("Cannot finish transaction for updating Employee no. " + id + " with an error " + e.Message); } } }