Entity Framework: There is already an open DataReader associated with this Command which must be closed first

前端 未结 5 1855
太阳男子
太阳男子 2021-02-06 02:58

This question is related to this:

My repository method has this code:

 public IEnumerable GetApplicationPositionHistor         


        
相关标签:
5条回答
  • 2021-02-06 03:15

    Have you tried adding MultipleActiveResultSets=true; to your connection string?

    0 讨论(0)
  • 2021-02-06 03:23

    Generally do not use EF object in view, but create a POCO object for the view model and map the query result on the view model. EF do not execute the query in your repository method because the query are not executed at definition time but only when you try to access the data. In your view you are using the same query many time and this is not correct.

    If you want to access the list of object returned by your repository method, use toList

    0 讨论(0)
  • 2021-02-06 03:30

    The real problem if that you are Lazy Loading the Position reference from the ApplicantPosition entity before the query ends this execution. If you want to keep the deferred execution on that scenario you can eager load the Position reference on your query like this:

    Include(o => o.applicantPosition.Select(a => a.Position));

    and on your GetApplicationPositionHistories keeps returning the IEnumerable.

    The other solution is to actually run the query on the GetApplicationPositionHistories method calling the ToList() or ToArrray() methods on the query.

    0 讨论(0)
  • 2021-02-06 03:33

    Quick solution :

    public IEnumerable<ApplicationPositionHistory> GetApplicationPositionHistories(int applicantId, int positionId)
        {
            return context.ApplicationsPositionHistory.Where(d => d.applicantPosition.ApplicantID == applicantId && d.applicantPosition.PositionID == positionId).Include(o => o.applicantPosition).ToList() ;
        }
    

    If you want to know, why this fixing your problem, read about how LINQ and deffered execution works. In few words - if you dont "force" execution of the select by "enumerating" query by ToList, it is in fact executed too late - in view. And this is causing trouble with other queries which want to use same connection.

    0 讨论(0)
  • 2021-02-06 03:34

    This error happens when a new query is going to be executed while you're in inside another query. Consider you have something like this in your view

    @Html.DisplayFor(modelItem => item.Device.Name)
    

    and in your Device model you have

        public string Name
        {
            get
            {
                return String.Format("{0} {1}", Brand.BrandName, Model.ModelName);
            }
        }
    

    then since for evaluating Device.Name it requires to query its Brand and Model it will become query inside query and so the solution is to enable MutlipleActiveResultSets in your database connection string as follows:

        <add name="MyDBContext" connectionString="Data Source=.;Initial Catalog=mydb;Integrated Security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
    
    0 讨论(0)
提交回复
热议问题