Skip to content

Instantly share code, notes, and snippets.

@manoj-choudhari-git
Created July 20, 2021 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manoj-choudhari-git/55e87c9ba37f0f77f7e0d7ec6f20ab2e to your computer and use it in GitHub Desktop.
Save manoj-choudhari-git/55e87c9ba37f0f77f7e0d7ec6f20ab2e to your computer and use it in GitHub Desktop.
.NET - EF Core - Transactions with SavePoints
static async Task Main(string[] args)
{
AddStudents();
}
static void AddStudents()
{
using var context = BuildUniversityContext();
using var transaction = context.Database.BeginTransaction();
try
{
context.Add(
new Student
{
FirstName = "John",
LastName = "Doe",
Address = "4 Privet Drive",
});
context.SaveChanges();
transaction.CreateSavepoint("FirstUserAdded");
context.Add(
new Student
{
FirstName = "Jane",
LastName = "Doe",
Address = "4 Privet Drive",
});
context.SaveChanges();
transaction.Commit();
}
catch(Exception ex)
{
transaction.RollbackToSavepoint("FirstUserAdded");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment