Esempio n. 1
0
 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()));
     }
 }
Esempio n. 2
0
        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();
                }
            }
        }