public void QueryWithParametersTest <T>() { // Locking this test code section to avoid puzzling exception with 'MySql': 'Only MySqlParameter objects may be stored'. if (Monitor.TryEnter(ParameterizedQueryLockObject)) { try { // Arrange var person = new Person { FamilyName = "Wagner", FirstName = "Kurt", PetCount = 6 }; // Create 'Person' to use in test. var student = new Student() { CourseCount = 6 }; // Create 'Student' to use in test. var efCodeFirstLibRepository = new EfCodeFirstLibRepository(); var createdPerson = efCodeFirstLibRepository.PersonCreate(person); // Create 'Person' in repository. student.PersonId = createdPerson.PersonId; var createdStudent = efCodeFirstLibRepository.StudentCreate(student); // Create 'Student' in repository. var personId = createdPerson.PersonId; // Act var studentList = efCodeFirstLibRepository.GetStudentListByPersonId(personId); DeleteEntity(createdStudent); // Cleanup: Attempt to delete here/now in case an assert fails. (Deleting 'Student' first due to 'PersonId' foreign key constraint.) DeleteEntity(createdPerson); // Cleanup: Attempt to delete here/now in case an assert fails. // Assert Assert.IsTrue(studentList.Any()); Assert.IsTrue(studentList.Count.Equals(1)); } catch (Exception exception) { System.Diagnostics.Debug.WriteLine(string.Format("Exception: {0}.", exception.ToString())); } finally { Monitor.Exit(ParameterizedQueryLockObject); } } }
private static void RunQueryWithParametersExample() { var person = new Person { FamilyName = "Jones", FirstName = "Sam", PetCount = 3 }; var student = new Student() { CourseCount = 7 }; var efCodeFirstLibRepository = new EfCodeFirstLibRepository(); var createdPerson = efCodeFirstLibRepository.PersonCreate(person); var personId = createdPerson.PersonId; student.PersonId = personId; var createdStudent = efCodeFirstLibRepository.StudentCreate(student); var studentList = efCodeFirstLibRepository.GetStudentListByPersonId(personId); }