Add example with transaction security with save points.
This commit is contained in:
parent
feafaba447
commit
f6d5733992
@ -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);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user