0

I have an issue when I call db.SaveChanges() method. Even if in the connectionString, I have MultipleActiveResultSets=True and I'm using ToList() method when I'm reading from database, when I call db.SaveChanges(), I'm getting the error from the title of the question.

Here is my connection string:

<connectionStrings><add name="DBEntities" connectionString="metadata=res://*/DBEntities.csdl|res://*/DBEntities.ssdl|res://*/DBEntities.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.\ServerName;initial catalog=DatabaseName;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /></connectionStrings>

Here is my c# code:

    db.SaveChanges();
    int errorTrans = (from el in this.curentHandbackFile.Root.Element("data").Elements("Transaction") where el.Attribute("rrc").Value.Contains("_NOK_") select el).Count();
                    IEnumerable<XAttribute> integratedTrans = from el in this.curentHandbackFile.Root.Element("data").Elements("Transaction").Attributes("rrc") where el.Value.Contains("_OK_") select el;
                    int warningTransactionsCount = integratedTrans.Where(trans => trans.Value != ReturnCodes.RecordCodes.TransactionImported.Key).ToList().Count;
                    int successTransactionsCount = integratedTrans.Where(trans => trans.Value == ReturnCodes.RecordCodes.TransactionImported.Key).ToList().Count;

_import.Successes = successTransactionsCount;
_import.Errors = errorTrans;
_import.Warnings = warningTransactionsCount;

db.SaveChanges();

when I call the second time db.SaveChanges(), I'm getting the error. Any idea why?

PS: I'm using .NET 3.5

rene
  • 41,474
  • 78
  • 114
  • 152
Buda Gavril
  • 21,409
  • 40
  • 127
  • 196
  • Tons of related question on the right side. Look at them. – Steve Jun 20 '14 at 08:50
  • I've read them but most of the answers suggested to put MultipleActiveResultSets=True in the connection string or to use tolist() method – Buda Gavril Jun 20 '14 at 08:51
  • You don't [execute a query while iterating over the results from another query](http://stackoverflow.com/a/6064422/578411)? – rene Jun 20 '14 at 08:54
  • No, I'm not iterating and I'm not using threads and in my project I have only one object DbModel and as you can see, when I call SaveChanges first time, it does not return an error, only when I call the methods second time... – Buda Gavril Jun 20 '14 at 09:08
  • Why are you calling SaveChanges more than once? Where do you instantiate the DbCOntext? – JotaBe Jun 20 '14 at 09:47
  • DbModel is instantiated in the main method of my Program.cs file. I called twice SaveChanges to isolate the code that may produce this error. – Buda Gavril Jun 20 '14 at 09:56

0 Answers0