Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何关闭与LINQ/EF的命令连接_C#_Entity Framework_Ado.net - Fatal编程技术网

C# 如何关闭与LINQ/EF的命令连接

C# 如何关闭与LINQ/EF的命令连接,c#,entity-framework,ado.net,C#,Entity Framework,Ado.net,使用Azure和EF时,我收到错误消息: DataReader associated with this Command which must be closed first. 这是因为我的查询是嵌套的: foreach (Element s in ElementSet.All()) { if (somecondition) { ElementSet.Add() } } 如何从ElementSet加载所有元素并在使用foreach循环之前关闭连接 注意:对于S

使用Azure和EF时,我收到错误消息:

 DataReader associated with this Command which must be closed first. 
这是因为我的查询是嵌套的:

foreach (Element s in ElementSet.All()) {
   if (somecondition) {  
     ElementSet.Add() 
   }
}
如何从ElementSet加载所有元素并在使用foreach循环之前关闭连接


注意:对于SQL azure,我无法在连接字符串中设置MARS,这将使服务器接受多个连接。

MARS不会使用多个连接,但它允许通过单个连接执行多个并发操作。所以你一定要打开它

如果要强制EF加载所有实体,请立即在查询中调用
ToList

foreach (Element s in ElementSet.All().ToList()) {
   if (somecondition) {  
     ElementSet.Add() 
   }
}