DB Transactions
Combining several operations into one transaction within the same context
using (var dbContextTransaction = db.Database.BeginTransaction(IsolationLevel.ReadCommitted))
{
try
{
// First SQL operation
int data = await db.Database.ExecuteSqlCommandAsync(..);
if (data > 0)
{
// Another SQL operation
await db.Database.ExecuteSqlCommandAsync(..);
}
else
{
response.isError = true;
response.message = Resources.Resources.StudentAlreadyAddedinClass;
}
}
catch (Exception ex)
{
_logger.Error($"$ValidStudentsForClass {ex.Message}");
response.isError = true;
response.message = Resources.Resources.ErrorOccurred;
dbContextTransaction.Rollback();
}
}Last updated