Last active
September 3, 2019 09:17
-
-
Save mizrael/4718a854aeac21273ad2a4969478626e to your computer and use it in GitHub Desktop.
an example of NOT to use a transaction with EF Core
This file contains hidden or 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
using (var tran = await _dbContext.BeginTransactionAsync()) | |
{ | |
try | |
{ | |
var modelsToRemove = await _dbContext.QueryModels | |
.Where(f => /* some filter here */) | |
.ToArrayAsync(); | |
if (modelsToRemove.Any()) | |
_dbContext.QueryModels.RemoveRange(modelsToRemove); | |
if (newModels?.Any()) | |
_dbContext.QueryModels.AddRange(newModels); | |
await _dbContext.SaveChangesAsync(); | |
tran.Commit(); | |
} | |
catch (Exception ex) | |
{ | |
tran.Rollback(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment