I need to write to the log table after transaction rollback in EF. The current code:
using (MyDataBase db = new DataBase())
{
using (var dbContextTransaction = db.Database.BeginTransaction())
{
try
{
//Inserting several records to different tables
var newEntity = new Entity();
...
db.Entity.Add(newEntity);
db.SaveChanges();
db.up_StoredProcCall;
dbContextTransaction.Commit();
}
catch (Exception ex)
{
dbContextTransaction.Rollback();
var logEntry = new LogEntry();
...
db.LogEntry.Add(logEntry);
db.SaveChanges();
}
}
}
It does not work as EF caches all inserts internally and flushes them all along with a log record in a single transaction on:
db.LogEntry.Add(logEntry);
db.SaveChanges();
What is the best way to achive the goal: