我在名为 TopicCount
的应用程序中向代码优先实体添加了一个简单的方法,它接受一个 bool 值。它计算导航属性中的项目数,并根据传入的 bool 值是真还是假对计数进行不同的过滤。
public class Board
{
public short BoardID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public bool Hidden { get; set; }
public bool ModsOnly { get; set; }
public bool Locked { get; set; }
public bool Anonymous { get; set; }
public bool AllTopics { get; set; }
public virtual ICollection<Topic> Topics { get; set; }
public long TopicCount(bool isModerator)
{
if (isModerator)
return this.Topics.ToList().Count;
else
return this.Topics
.Where(x => !x.ModsOnly)
.Where(x => !x.Board.ModsOnly)
.Where(x => !x.Board.Hidden)
.Count();
}
}
当我调用此 TopicCount
方法时,它失败了(无论 bool 是真还是假)并出现以下错误:
There is already an open DataReader associated with this Command which must be closed first.
有什么想法吗?
最佳答案
这也是在单个上下文中流式传输多个命令结果的症状(延迟加载)。您可以在连接字符串中设置“MultipleActiveResultSets=True”来解决这个问题。
关于asp.net - Entity Framework 代码首先已经打开数据阅读器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12518703/