Unbreakable
Unbreakable

Reputation: 8084

What is reason of getting "There is already an open DataReader associated with this Command which must be closed first" in ASP.Net MVC 5 project

I am a beginner and learning Asp.Net MVC 5 web development. In one of my views I am getting exception as

{"There is already an open DataReader associated with this Command which must be closed first."}

Model:

public class CoverLetter
{
    [Key]
    public int CoverLetterId { get; set; }

    [Required]
    [Display(Name = "Cover Letter")]
    [StringLength(255)]
    public string CoverLetterName { get; set; }

    [Display(Name = "Company Name")]
    [Required]
    public int CompanyId { get; set; }
    public virtual Company Company { get; set; }

    [Required]
    public string CandidateId { get; set; }
    public virtual ApplicationUser Candidate { get; set; }
}

View:

@model IEnumerable<Bridge.Models.CoverLetter>
@using Bridge.ViewModels

<div class="panel panel-default">
    <table class="table">
        <thead>
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.CoverLetterName)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Company.CompanyName)
                </th>
            </tr>
        </thead>
            foreach (var item in Model)
            {
                <tbody>
                    <tr>
                        <td>
                            @Html.DisplayFor(modelItem => item.datetime)
                        </td>
            @*In below code exception comes*@
                        <td>
                            @Html.DisplayFor(modelItem => item.Company.CompanyName)
                        </td>
                          </tr>
                </tbody>
            }
    </table>
</div>

Controller:

public ActionResult CoverLetterCenter()
{
    var candidateId = User.Identity.GetUserId();
    var coverLetterList = _context.CoverLetters
        // .Include("Company")
        .Where(r => r.CandidateId == candidateId).OrderByDescending(r => r.datetime);

    return View(coverLetterList);
}

Point 1:

If I add .Include("Company") in my controller action then the exception goes away. Can someone please tell me about this behavior.

Point 2:

Also note that, when I debug and hover Over the coverLetterList variable, then it always have the Company variable populated with the data, irrespective of whether I have added .Include("Company") or not. Then why am I getting exception.

Upvotes: 1

Views: 619

Answers (1)

Aaron Roberts
Aaron Roberts

Reputation: 1372

Entity Framework and LINQ in general use Lazy evaluation for Enumerable collections. For the controller code there is no need to evaluate the enumeration since it is not being used until the code gets to the view rendering. By debugging and hovering over the data in the debug window the evaluation is performed and thus you see the company data. You can add a single method chain to fix this.

public ActionResult CoverLetterCenter()
{
    var candidateId = User.Identity.GetUserId();
    var coverLetterList = _context.CoverLetters
        // .Include("Company")
        .Where(r => r.CandidateId == candidateId).OrderByDescending(r => r.datetime).ToList();

    return View(coverLetterList);
}

Upvotes: 1

Related Questions