public void test_insert_multiple_employees_without_any_explicit_transaction_scope_should_be_saved() { //Arrange using (ICommandRepository <Department> departmentCommandRepository = GetCommandRepositoryInstance <Department>()) using (ICommandRepository <Employee> employeeCommandRepository = GetCommandRepositoryInstance <Employee>()) using (IQueryableRepository <Department> departmentQueryableRepository = GetQueryableRepositoryInstance <Department>()) using (IQueryableRepository <Employee> employeeQueryableRepository = GetQueryableRepositoryInstance <Employee>()) { //Arrange Department departmentFake = FakeData.GetDepartmentFake(); Employee managerEmployeeFake = FakeData.GetEmployeeFake(); managerEmployeeFake.EmployeeName = "XYZ"; managerEmployeeFake.DeptID = departmentFake.Id; managerEmployeeFake.Department = departmentFake; Employee subEmployeeFake = FakeData.GetEmployeeFake(2); subEmployeeFake.DeptID = departmentFake.Id; subEmployeeFake.Department = departmentFake; subEmployeeFake.ManagerId = managerEmployeeFake.Id; subEmployeeFake.Manager = managerEmployeeFake; //Action //Considering cascading inserts, here the order in which the list is inserted with different employee //objects is important. Since department object is defined as required in the EmployeMap class, //Effort(just like EF) automatically inserts the department object before inserting the employee objects. //But again in EmployeeMap class, Manager is marked as optional, so manager and the employees under him or her needs to //be explicitly inserted in proper order.Also here, EF doesn't ensure that if managerEmployeeFake gets //saved successfully and then subEmployeeFake doesn't get saved, then all the operations will be //rolled back (at the DB level). //And all these(inserting department object first, then manager object and then sub emloyee object), happens // internally within the same transaction (just like EF6) and one doesn't need to use the UnitOfWork class // (available in this project). employeeCommandRepository.Insert(new List <Employee> { managerEmployeeFake, subEmployeeFake }); //Assert departmentQueryableRepository.Count().Should().Be(1); employeeQueryableRepository.Count().Should().Be(2); employeeQueryableRepository.First().EmployeeName.Should().Be("XYZ"); employeeQueryableRepository.Single(x => x.EmployeeName == "Sandip").Id.Should().Be(2); // LINQ to Entities doesn't support the method "Last" and so will throw an exception.That's one of // the differences w.r.t LINQ to Objects.If tested with some mock(s) then the below code won't throw any // exception since internally mocks uses LINQ to Objects (atleast as far as LINQ in general is concerned) // and so true behaviour won't reflect using mocks. Action action = () => employeeQueryableRepository.Last(); action.ShouldThrow <NotSupportedException>(); }; }