Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
C#
public static bool DeleteAll()
        {
            try
            {
                Entities db = new Entities();
                IEnumerable<CMPS285_table_schedule> temp = (from p in db.CMPS285_table_schedule select p);
                foreach (CMPS285_table_schedule item in temp)
                {
                    db.DeleteObject(item);
                    db.SaveChanges();
                }
            }
            catch (Exception E)
            {
                Console.WriteLine(E);
            }
            
            return false;
        }

I am getting the error:
"New transaction is not allowed because there are other threads running in the session."


Need help.
Posted
Updated 11-Mar-12 8:14am
v2
Comments
André Kraak 11-Mar-12 14:14pm    
Edited question:
Added pre tags

C#
public static bool DeleteAll()
        {
            try
            {
                using (Entities db = new Entities())
                {
                   var temp = (from p in db.CMPS285_table_schedule select p).ToList();
                   foreach (var item in temp)
                   {
                      db.DeleteObject(item);
                      db.SaveChanges();
                   }
                }
            }
            catch (Exception E)
            {
                Console.WriteLine(E);
            }
            
            return false;
        }


EF is still in the original transaction to retrieve all the data records in Table. So when we call .SaveChanges() to execute the SQL statements in another transaction, we receive the exception.

To workaround the issue, I would recommend you retrieve the results in one go before the foreach loop.

Also please take a look at this link below on deferred and immediate execution.
http://www.dotnetcurry.com/ShowArticle.aspx?ID=750[^]
Please vote and mark as solution thanks.
 
Share this answer
 
Comments
ahatteri 12-Mar-12 0:02am    
Still has some error

"System.Data.UpdateException: Unable to update the EntitySet 'CMPS285_table_schedule' because it has a DefiningQuery and no <deletefunction> element exists in the <modificationfunctionmapping> element to support the current operation."

Aite I am completely new to database and I am working on this not knowing anything. Visual Studio made the connection and I have been creating db = new Entities() on other methods as well but for test purpose non are being executed. What would u prefer to make it working not my way bu any other way.

I tried passing query to my online database; that worked but was too slow. Need Help
Dean Oliver 12-Mar-12 3:14am    
take a look at this link below. You have to remove the defining query element.
http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/220262bd-85d4-4f29-96a8-4a7d1d2c8293

This will explain where this is
http://msdn.microsoft.com/en-us/library/cc982038.aspx
Just wanted to add how i fixed it.
items to delete or add should be passed as parameter/reference - in my case it was the problem. I passed an object to the delete method and tried to delete it - ERROR. then I created an object with same parameter and deleted it. HURRAY!!! the same method as above worked.

instead of Ieneurable bla blah var is preffered. I just dont know why but it works
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900