//[ExpectedException(typeof(NHibernate.TransactionException))] //Cannot delete employee, you can only set not surrent public void CanDeleteEmployee() { //here we add a new employee and delete the employee to stop the referencial integrity kicking in using (var session = sessionFactory.OpenSession()) { var rnd = new Random(); int employeeId = 0; var employee = new DAL.Entities.HumanResources.Employee() { JobTitle = "Sales Representative", MaritalStatus = "M", Gender = "F", NationalIDNumber = rnd.Next().ToString(), HireDate = DateTime.Now.AddYears(-2), BirthDate = DateTime.Now.AddYears(-30), LoginId = "adventureworks\\" + rnd.Next().ToString(), //Person properties Title = "Mr", FirstName = "EmployeeFirstName01", LastName = "employeeLastName01", PersonType = "EM" }; using (var transaction = session.BeginTransaction()) { try { session.Save(employee); transaction.Commit(); employeeId = employee.Id; Assert.IsTrue(employeeId > 0); } catch (Exception ex) { transaction.Rollback(); throw ex; } } //Now delete the created employee var employeeToDelete = session.CreateCriteria <Entities.HumanResources.Employee>().List <Entities.HumanResources.Employee>().Where(e => e.Id == employeeId).First(); using (var transaction = session.BeginTransaction()) { try { session.Delete(employeeToDelete); transaction.Commit(); } catch (Exception ex) { Assert.IsTrue(ex.InnerException.Message.Contains("The transaction ended in the trigger. The batch has been aborted")); } } } }
public void CanAddEmployeeDepartmentHistory() { //for testing we add a new employee and then assign the top most department and top most shift to that new employee var rnd = new Random(); var employee = new DAL.Entities.HumanResources.Employee { JobTitle = "Sales Representative", MaritalStatus = "M", Gender = "F", NationalIDNumber = rnd.Next().ToString(), HireDate = DateTime.Now.AddYears(-2), BirthDate = DateTime.Now.AddYears(-30), LoginId = "adventureworks\\" + rnd.Next().ToString(), //Person properties Title = "Mr", FirstName = "TestFirstName05", LastName = "TestLastName05", PersonType = "EM" }; var session = sessionFactory.OpenSession(); employee.AddEmployeeDepartmentHistory(new Entities.HumanResources.EmployeeDepartmentHistory { Department = session.CreateCriteria <Entities.HumanResources.Department>().List <Entities.HumanResources.Department>()[0], Shift = session.CreateCriteria <Entities.HumanResources.Shift>().List <Entities.HumanResources.Shift>()[0], StartDate = DateTime.Now }); using (ITransaction transaction = session.BeginTransaction()) { try { session.SaveOrUpdate(employee); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); throw ex; } } }
public void CanAddEmployee() { using (var session = sessionFactory.OpenSession()) { var rnd = new Random(); var employee = new DAL.Entities.HumanResources.Employee() { JobTitle = "Sales Representative", MaritalStatus = "M", Gender = "F", NationalIDNumber = rnd.Next().ToString(), HireDate = DateTime.Now.AddYears(-2), BirthDate = DateTime.Now.AddYears(-30), LoginId = "adventureworks\\" + rnd.Next().ToString(), //Person properties Title = "Mr", FirstName = "TestFirstName04", LastName = "TestLastName04", PersonType = "EM" }; using (var transaction = session.BeginTransaction()) { try { session.Save(employee); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); throw ex; } } } }