I am building a .NET 4.0 MVC3 app. I use Entity Framework 4.1 and Ninject to inject my repository to controllers:
Bind(Of IDataContext).To(Of DataContext)().InRequestScope()
I have a controller with action that utilizes MyObject’s (POCO entity) static function and pass a repository object to it. This function is supposed to check whether a db object is filled with correct data or not (and return Boolean):
Dim isComplete as Boolean = MyObject.IsComplete(_context, id, qid)
IsComplete (it is a dummy for now) function looks as follows:
Public Shared Function IsComplete(context As IDataContext, id As Integer, qid As Integer) As Boolean
Dim lookUp As Lookup(Of Integer, Integer) =
context.QuestionBlockAnswers.Where(Function(m) (m.BlockId = id AndAlso m.QuestionAnswer.QuestionField.Question.QuestionGroup.QuestionnaireVersion.QuestionnaireId = qid)).ToLookup(Function(m) m.QuestionAnswer.QuestionField.Question.Id, Function(m) m.QuestionAnswerId)
Return True
End Function
As you can see it only initializes a lookUp variable and returns true. The problem is that this lookUp gives my hard times. If I call the action (I use ajax call in this case), I receive an error:
[InvalidOperationException]: There is already an open DataReader associated with this Command which must be closed first.
If i comment the initialization of the lookUp variable I do not get the error anymore. It’s also not a problem with passing the context as a parameter to a static function, because I do it for some other objects and it works fine. It fails only in this case when ToLookup is used.
Do you have any ideas how I can fixed this problem?
You need to add
MultipleActiveResultSets=true;to your connection string.This is a requirement for EF to work properly .