大家看看下面两段代码(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?"异常,有人知道为什么会出现异常吗?
这是没有关闭数据库连接,个人推测可能是EF底层共用一个sqlconnection或lz你的代码产生了错误,建议整体再检查下。
EF底层是共用一个sqlconnection的,我想弄明白的是为什么第二段代码不能正常运行
@artwl: 求真相
检查一下EF实际生成的SQL
我的也出现了类似的问题,检查一下就是数据库处理不当造成的,有待改进啊 !!