Categories
Database Development

Nested transactions are not supported entity framework

I’m trying to wrap up saving data to tables in a transaction like so :

         using (var context = new StDbContext())
         {
            context.Database.Log = Console.Write;
            context.Database.Connection.Open();
            using (DbTransaction transaction = context.Database.Connection.BeginTransaction())
            {
               try
               {
                  var unit_of_work = new UnitOfWork(context);
                  unit_of_work.Sessions.AddModelSession();
                  context.SaveChanges();

                  transaction.Commit();
               }
               catch (Exception ex)
               {
                  transaction.Rollback();
                  TestBaseCore.mNLogLoggerService.Error(ex.Message);
               }
            }
         }

Inside unit_of_work.Sessions.AddModelSession(); I’m saving data(UnitOfWork.Cities.AddCity(); ..and so on)-> multiple SaveChanges() also;
But I’m getting the following error : “Nested transactions are not supported entity framework”

Is there a better way of using translations with multiple SaveChanges? or can I optimize my code in some way to fix this issue>

Leave a Reply

Your email address will not be published. Required fields are marked *