Add example with transaction security with save points.

This commit is contained in:
Kevin Matsubara 2025-04-14 21:49:44 +02:00
parent feafaba447
commit f6d5733992

View File

@ -23,6 +23,56 @@ context.Database.EnsureCreated();
// var firstCoach = await context.Coaches.FirstOrDefaultAsync(); // var firstCoach = await context.Coaches.FirstOrDefaultAsync();
async Task TransactionWithSavepoint()
{
var transaction = context.Database.BeginTransaction();
var league = new League
{
Name = "Geosphere league"
};
context.Add(league);
context.SaveChanges();
transaction.CreateSavepoint("CreatedLeague");
var coach = new Coach
{
Name = "Brad Ironfist"
};
context.Add(coach);
context.SaveChanges();
var teams = new List<Team>
{
new Team
{
Name = "Dominators",
LeagueId = league.Id,
CoachId = coach.Id
}
};
context.AddRange(teams);
context.SaveChanges();
try
{
transaction.Commit();
}
catch (Exception)
{
// Rollback everything.
//transaction.Rollback();
// Or rollback to a specific save point.
transaction.RollbackToSavepoint("CreatedLeague");
throw;
}
}
async Task UserDefinedQuery() async Task UserDefinedQuery()
{ {
var earliestMatch = context.GetEarliestTeamMatch(1); var earliestMatch = context.GetEarliestTeamMatch(1);