When using many methods of the ViewModel/TableModel class you'll notice that
you can specify a database connection/transaction.
When specified, commands will be executed against the given connection/transaction
instead of creating a new connection to the database. This is specially useful when
having to perform multiple database operations in a section of code, or when it's
necessary that ALL the commands are successfully executed.
MagnaDB also provides utility methods that let you create connections/transactions using
any given model's connection string. This will be shown in the examples below.
Connections example
using System;
namespace TestMagnaDB
{
class Program
{
static void Main(string[] args)
{
using (var connection = Cat.CreateOpenConnection())
{
Cat missus = new Cat() { Name = "Misulindo", Breed = "Ragdoll" };
Cat chungus = new Cat() { Name = "Hector", Breed = "Persian" };
try
{
missus.Insert(connection);
chungus.Insert(connection);
}
finally
{
connection.Close();
}
}
}
}
}
using System;
namespace TestMagnaDB
{
class Program
{
static void Main(string[] args)
{
using (var transaction = Cat.CreateActiveTransaction())
{
Cat missus = new Cat() { Name = "Misulindo", Breed = "Ragdoll" };
Cat chungus = new Cat() { Name = "Hector", Breed = "Persian" };
try
{
missus.Insert(transaction);
chungus.Insert(transaction);
Always make sure to commit or rollback your transactions
transaction.Commit();
}
catch
{
transaction.Rollback();
}
}
}
}
}