Technically speaking, you could write something like this (because if transaction
is null
, no auto-dispose will happen as the result of using
, see http://msdn.microsoft.com/en-us//library/yh598w02.aspx):
using (var transaction = Transaction.Current == null ? CreateTransaction() : null)
{
_dbSet.Attach(item);
_dbSet.Remove(item);
_context.SaveChanges();
if(transaction != null)
transaction.Complete();
}
(I'm not sure about the check. In the original code Transaction.Current != null
, however Transaction.Current == null
looks more logical.)
But I would consider just grouping
_dbSet.Attach(item);
_dbSet.Remove(item);
_context.SaveChanges();
into a separate function.