I try use function begin transaction but can not work
I use dotConnect for Mysql and EF Core 1.1.0
this my code :
Code: Select all
public DbTransaction BeginTransaction()
{
if (null == transaction)
{
if (_context.Database.GetDbConnection().State != ConnectionState.Open)
{
_context.Database.OpenConnection();
}
this.transaction = (DbTransaction) _context.Database.CurrentTransaction;
_context.Database.UseTransaction(transaction);
}
return transaction;
}
Code: Select all
using (var transaction = this._unitOfWork.BeginTransaction())
{
try {
this._unitOfWork.OrderTypeRepository.Add(new Tbordertype
{
OrderTypeID = ordertype.OrderTypeId,
OrderTypeName = ordertype.OrderTypeName
});
this._unitOfWork.OrderTypeRepository.Save();
transaction.Commit();
}
catch (Exception) {
transaction.Rollback();
}
}
The database save data before commit
How to use Begin Transaction
Please help me.