public void ShouldBeAbleToPersistAndRecall() { using (var db = new TempDBLocalDb()) { var runner = new MigrationsRunner(db.ConnectionString, Console.WriteLine); runner.MigrateToLatest(); //---------------Set up test pack------------------- //---------------Assert Precondition---------------- //---------------Execute Test ---------------------- // first, put your left leg in var item= ItemBuilder.BuildRandom(); using (var context = new LendingLibraryContext(db.CreateConnection())) { context.Items.Add(item); context.SaveChangesWithErrorReporting(); } // shake it all about using (var context = new LendingLibraryContext(db.CreateConnection())) { var persisted = context.Items.FirstOrDefault(o => o.ItemId == item.ItemId); Assert.IsNotNull(persisted); Assert.AreEqual(item.ItemId, persisted.ItemId); Assert.AreEqual(item.Description, persisted.Description); Assert.AreEqual(item.Mimetype, persisted.Mimetype); Assert.AreEqual(item.Photo, persisted.Photo); } //---------------Test Result ----------------------- } }
public void StaticConstructor_ShouldNotAllowEntityMigrations() { //---------------Set up test pack------------------- using (var tempDb = new TempDBLocalDb()) using (var dbConnection = tempDb.CreateConnection()) using (var context = new LendingLibraryContext(dbConnection)) { try { // force Entity to do whatever it does when it // spins up but don't care if the query onto Persons // fails (which it will at time of writing because // we haven't written any migrations to create the // table) context.People.ToArray(); } catch { } //---------------Assert Precondition---------------- //---------------Execute Test ---------------------- using (var cmd = dbConnection.CreateCommand()) { cmd.CommandText = @"select * from INFORMATION_SCHEMA.TABLES where TABLE_NAME = '__MigrationHistory';"; using (var reader = cmd.ExecuteReader()) { //---------------Test Result ----------------------- Assert.IsFalse(reader.Read()); } } } }
public void ShouldBeAbleToPersistAndRecall() { using (var db = new TempDBLocalDb()) { var runner = new MigrationsRunner(db.ConnectionString, Console.WriteLine); runner.MigrateToLatest(); //---------------Set up test pack------------------- //---------------Assert Precondition---------------- //---------------Execute Test ---------------------- // first, put your left leg in var person = CreatePerson(); using (var context = new LendingLibraryContext(db.CreateConnection())) { context.People.Add(person); context.SaveChangesWithErrorReporting(); } // shake it all about using (var context = new LendingLibraryContext(db.CreateConnection())) { var persisted = context.People.FirstOrDefault(o => o.PersonId == person.PersonId); Assert.IsNotNull(persisted); Assert.AreEqual(person.Email, persisted.Email); Assert.AreEqual(person.FirstName,persisted.FirstName); Assert.AreEqual(person.PersonId,persisted.PersonId); Assert.AreEqual(person.Surname,persisted.Surname); Assert.AreEqual(person.PhoneNumber,persisted.PhoneNumber); Assert.AreEqual(person.Photo,persisted.Photo); } //---------------Test Result ----------------------- } }
public LendingLibraryContext Init() { return(_dbContext ?? (_dbContext = new LendingLibraryContext())); }
public void ParameterlessConstructor_ShouldUseConnectionStringCalled_DefaultConnection() { //---------------Set up test pack------------------- //---------------Assert Precondition---------------- using (var tempDb = new TempDBLocalDb()) { //---------------Execute Test ---------------------- var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); var connectionStringsSection =(ConnectionStringsSection)config.GetSection("connectionStrings"); connectionStringsSection.ConnectionStrings["DefaultConnection"].ConnectionString = tempDb.ConnectionString; config.Save(); ConfigurationManager.RefreshSection("connectionStrings"); var sut = new LendingLibraryContext(); //---------------Test Result ----------------------- var conn = sut.Database.Connection; Assert.AreEqual(tempDb.ConnectionString, conn.ConnectionString); } }