Created
July 20, 2021 20:41
-
-
Save manoj-choudhari-git/55e87c9ba37f0f77f7e0d7ec6f20ab2e to your computer and use it in GitHub Desktop.
.NET - EF Core - Transactions with SavePoints
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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