使用MultipleActiveResultSets = True和ToList()仍然会抛出与此命令关联的已打开DataReader

时间:2014-06-20 08:48:50

标签: c# entity-framework exception

当我调用db.SaveChanges()方法时,我遇到了问题。即使在connectionString中,当我从数据库中读取时,我有MultipleActiveResultSets=True并且我使用ToList()方法,当我拨打db.SaveChanges()时,我就是从问题的标题中得到错误。

这是我的连接字符串:

<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>

这是我的c#代码:

    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();

当我第二次拨打db.SaveChanges()时,我收到了错误消息。知道为什么吗?

PS:我使用的是.NET 3.5

0 个答案:

没有答案