Error: New transaction is not allowed because there are other threads running in the session in entity framework.


Code: Where Error was coming

                        foreach (Objects Objtmp in tblObj)
                        {
                            ObjectMapping tblObjMap = new ObjectMapping
                            {
                                companyid = TblComp.CompanyId,
                                ObjectID = Objtmp.ObjectID,

                            };
                          
                            db.ObjectMapping.Add(tblObjMap);
                            db.SaveChanges();

 

Resolved Code : was as below.


                        var lstObj = tblObj.ToArray<Objects>();
                        
                        foreach(var lobj in lstObj)
                        {
                            ObjectMapping tblObjMap = new ObjectMapping();
                            tblObjMap.ObjectID= lobj.ObjectID;
                            tblObjMap.companyid=TblComp.CompanyId;
                            db.ObjectMapping.Add(tblObjMap);
                            db.SaveChanges();


                        }

 

What I did was I took the values of my search query into list variable and then do the updation.