3

I am getting an error in my code that says

An error occurred while starting a transaction on the provider connection. See inner exception for details

The inner exception is:

New transaction is not allowed because there are other threads running in the session.

My Code:

        using (var db = new ProductionContext())
        {
            var objct = ((System.Data.Entity.Infrastructure.IObjectContextAdapter)db).ObjectContext;
            objct.ExecuteStoreCommand("TRUNCATE TABLE [MU By Machine]");
            db.SaveChanges();

            var query =
                db.MU_Reports
                .GroupBy(x => new { x.Date, x.Machine_Number, x.Shift })
                .Select(x => new
                {
                    Date = x.Key.Date,
                    Shift = x.Key.Shift,
                    MachineNum = x.Key.Machine_Number,
                    MU = x.Sum(i => i.MU * 100)
                });

            foreach (var item in query)
            {
                var y = new MU_By_Machine();

                y.Date = item.Date;
                y.Shift = item.Shift;
                y.Machine_Number = item.MachineNum;
                y.MU = item.MU;

                db.MU_By_Machines.Add(y);
                db.SaveChanges();
            }

It happens at the db.SaveChanges portion of my code and when I move it out of the for loop the code works but only saves one line in my table. If additional information is required let me know. I am still new to c#.

JCM
  • 151
  • 8
  • "See inner exception for details" - What does the `InnerException` say? `ex.ToString()` is a handy way to show lots of gory detail about an exception. – D Stanley Jul 05 '16 at 13:26
  • @DStanley It says new transaction is not allowed because there are other threads running in the session – JCM Jul 05 '16 at 13:29
  • 1
    Possible duplicate of [SqlException from Entity Framework - New transaction is not allowed because there are other threads running in the session](http://stackoverflow.com/questions/2113498/sqlexception-from-entity-framework-new-transaction-is-not-allowed-because-ther) – trailmax Jul 05 '16 at 13:48
  • and http://stackoverflow.com/questions/10096509/new-transaction-is-not-allowed-because-there-are-other-threads-running-in-the-se – trailmax Jul 05 '16 at 13:49
  • and http://stackoverflow.com/questions/15363242/entity-framework-new-transaction-is-not-allowed-because-there-are-other-threads – trailmax Jul 05 '16 at 13:49

1 Answers1

2

Replace

 var query = db.MU_Reports
                .GroupBy(x => new { x.Date, x.Machine_Number, x.Shift })
                .Select(x => new
                {
                    Date = x.Key.Date,
                    Shift = x.Key.Shift,
                    MachineNum = x.Key.Machine_Number,
                    MU = x.Sum(i => i.MU * 100)
                });

with

 var query = db.MU_Reports
                .GroupBy(x => new { x.Date, x.Machine_Number, x.Shift })
                .Select(x => new
                {
                    Date = x.Key.Date,
                    Shift = x.Key.Shift,
                    MachineNum = x.Key.Machine_Number,
                    MU = x.Sum(i => i.MU * 100)
                }).ToList();

It helped me in similar issues. This error happens when you loop over a query result without using the .ToList() method. This method closes the connection and avoids this problem.

Piero Alberto
  • 3,823
  • 6
  • 56
  • 108