Using Transactions
Last updated
[Database]
public class Person {}
[Database]
public class Animal {}
class Program
{
static void Main()
{
Db.Scope(() =>
{
new Person();
Db.Scope(() =>
{
Transaction.Current.Commit(); // Commits the Person
new Animal();
});
Transaction.Current.Commit(); // Commits the Animal
});
}
}using Starcounter;
using System.Linq;
[Database]
public class Person {}
[Database]
public class Animal
{
public string Specie { get; set; }
}
class Program
{
static void Main()
{
Db.Scope(() =>
{
new Person();
Db.Transact(() =>
{
new Animal() { Specie = "Dog" };
}); // Animal is commited to the database - transaction is done
// The Animal committed can be accessed in the outer transaction
var animal = Db.SQL("SELECT a FROM Animal a").First();
// Rolls back the Person but not the Animal
Transaction.Current.Rollback();
});
}
}Db.Transact(() =>
{
Db.Scope(() => // SCERR4031
{
new Person();
});
}); using Starcounter;
[Database]
public class Person {}
[Database]
public class Animal {}
class Program
{
static void Main()
{
Db.Transact(() =>
{
Db.Transact(() =>
{
new Person();
}); // Person is not commited
new Animal();
}); // Animal and Person are commited
}
}The transaction is readonly and cannot be changed to write-mode. (ScErrReadOnlyTransaction (SCERR4093))[Database]
public class Person {}
class Program
{
static void Main()
{
new Person(); // SCERR4093
}
}[Database]
public class Person {}
class Program
{
static void Main()
{
Db.Transact(() => new Person());
}
}