Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
.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