private void PopulateData() { using (BodyShopContext context = new BodyShopContext()) { // Clean databases context.Database.EnsureDeleted(); MySQLTestStore.DeleteDatabase("01cars"); MySQLTestStore.DeleteDatabase("02bodyshops"); MySQLTestStore.DeleteDatabase("03employees"); context.Database.EnsureCreated(); context.BodyShop.AddRange(new BodyShop { Name = "Western Collision Works", City = "Hollywood", State = "California", Brand = "Chevrolet" }, new BodyShop { Name = "Yosemite Auto Body Shop", City = "Pico-Union", State = "California", Brand = "Ford" }); context.Car.AddRange(new Car { LicensePlate = "ATD 427", Make = "Chevrolet", Model = "Cruze", State = "New" }, new Car { LicensePlate = "TJC 265", Make = "Fore", Model = "Escape", State = "Used" }); context.Employee.AddRange(new Employee { FirstName = "Jonh", LastName = "Doe", DisplayName = "Jonhy", Timestamp = DateTime.Now }, new Employee { FirstName = "Maurice", LastName = "Kent", DisplayName = "Mau", Timestamp = DateTime.Now }); context.SaveChanges(); } }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { // get the class name of the caller to get a unique name for the database if (!optionsBuilder.IsConfigured || optionsBuilder.Options.FindExtension <MySQLOptionsExtension>() == null) { optionsBuilder.UseMySQL(MySQLTestStore.GetContextConnectionString(this.GetType())); } }
public void CanUseOptionsInDbContextCtor() { using (var context = new OptionsContext(new DbContextOptions <OptionsContext>(), new MySqlConnection(MySQLTestStore.CreateConnectionString("db-optionsindbcontext")))) { context.Database.EnsureCreated(); Assert.True(context.Blogs.Count() == 0); context.Database.EnsureDeleted(); } }
public void TransactionScopeTest() { using (var context = new SakilaLiteUpdateContext()) { context.InitContext(false); } using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted })) { using (MySqlConnection connection = new MySqlConnection(MySQLTestStore.GetContextConnectionString <SakilaLiteUpdateContext>())) { connection.Open(); MySqlCommand command = connection.CreateCommand(); command.CommandText = "DELETE FROM actor"; command.ExecuteNonQuery(); var options = new DbContextOptionsBuilder <SakilaLiteUpdateContext>() .UseMySQL(connection) .Options; using (TransactionScope innerScope = new TransactionScope(TransactionScopeOption.Required)) { using (var context = new SakilaLiteUpdateContext(options)) { context.Actor.Add(new Actor { FirstName = "PENELOPE", LastName = "GUINESS" }); context.SaveChanges(); innerScope.Complete(); } } // Commit transaction if all commands succeed, transaction will auto-rollback // when disposed if either commands fails scope.Complete(); } } }
public void TransactionTest() { using (var context = new SakilaLiteUpdateContext()) { context.InitContext(false); } using (MySqlConnection connection = new MySqlConnection(MySQLTestStore.GetContextConnectionString <SakilaLiteUpdateContext>())) { connection.Open(); using (MySqlTransaction transaction = connection.BeginTransaction()) { MySqlCommand command = connection.CreateCommand(); command.CommandText = "DELETE FROM actor"; command.ExecuteNonQuery(); var options = new DbContextOptionsBuilder <SakilaLiteUpdateContext>() .UseMySQL(connection) .Options; using (var context = new SakilaLiteUpdateContext(options)) { context.Database.UseTransaction(transaction); context.Actor.Add(new Actor { FirstName = "PENELOPE", LastName = "GUINESS" }); context.SaveChanges(); } transaction.Commit(); } } }