首页 新闻 搜索 专区 学院

There is already an open DataReader associated with this Command which must be closed first?

1
悬赏园豆:50 [已解决问题] 解决于 2012-04-14 17:04

大家看看下面两段代码(Entiry Framework):

//first
public List<Content> GetContentsByTagNameType(Guid appGuid, Guid userId)
{
var contents = this.Entities.Where(c => c.AppGuid == appGuid && c.UserId == userId );
if (userId == Guid.Empty)
{
contents = this.Entities.Where(c => c.AppGuid == appGuid );
}
var contentIds = contents.ToList();
return contentIds;
}

//second
public List<Content> GetContentsByTagNameType(Guid appGuid, Guid userId)
{
Func<Content, bool> predicate = c => c.AppGuid == appGuid && c.UserId == userId;
if (userId == Guid.Empty)
{
predicate = c => c.AppGuid == appGuid && c.Type == type;
}
var contentIds = this.Entities.Where(predicate).ToList();
return contentIds;
}

第一段代码可以正常运行,第二段代码会抛出"There is already an open DataReader associated with this Command which must be closed first?"异常,有人知道为什么会出现异常吗?

artwl的主页 artwl | 专家六级 | 园豆:16526
提问于:2012-03-12 15:21
< >
分享
最佳答案
0

这是没有关闭数据库连接,个人推测可能是EF底层共用一个sqlconnection或lz你的代码产生了错误,建议整体再检查下。

收获园豆:50
az235 | 大侠五级 |园豆:8283 | 2012-03-12 16:32

EF底层是共用一个sqlconnection的,我想弄明白的是为什么第二段代码不能正常运行

artwl | 园豆:16526 (专家六级) | 2012-03-12 16:34

@artwl: 求真相

az235 | 园豆:8283 (大侠五级) | 2012-03-12 16:38
其他回答(2)
0

检查一下EF实际生成的SQL

dudu | 园豆:38832 (高人七级) | 2012-03-13 17:28
0

我的也出现了类似的问题,检查一下就是数据库处理不当造成的,有待改进啊 !!

郑陆伟 | 园豆:202 (菜鸟二级) | 2014-01-02 09:00
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册