【问题标题】:Exception while index view render索引视图渲染时出现异常
【发布时间】:2021-06-16 08:32:07
【问题描述】:

索引视图渲染时抛出此异常:

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

这是索引视图:

@foreach (var item in Model) {
<tr>
    <td>

        @Html.DisplayFor(modelItem => item.pageGroup.GroupTitle)

    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Title)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.ShortDescription)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Text)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.ShowInSlider)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.ImageName)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Visit)
    </td>
    <td>
       
        @item.CreateData.ToShamsi();
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id=item.PageID }) |
        @Html.ActionLink("Details", "Details", new { id=item.PageID }) |
        @Html.ActionLink("Delete", "Delete", new { id=item.PageID })
    </td>
</tr> }

以及这一行中抛出的异常:

@Html.DisplayFor(modelItem => item.pageGroup.GroupTitle)

问题出在哪里?

如果你感到困惑,告诉我把你想要的部分的代码放在上面

这是我的连接字符串:

<connectionStrings>
<add name="MyCmsContext" connectionString="data source=.;    initial catalog=MyCmsDB;persist security info=True;     Integrated Security=True; " providerName="System.Data.SqlClient" MultipleActiveResultSets="true"  />

【问题讨论】:

    标签: asp.net asp.net-mvc


    【解决方案1】:

    您需要在连接字符串中允许 MARS。将MultipleActiveResultSets=true 添加到连接字符串的提供者部分。

    "Server=xxxx;Database=xxxxx;xxxxx MultipleActiveResultSets=true"
    

    【讨论】:

    • 感谢您的回答。我添加了您所说的代码,但我的上下文引发了新异常(System.TypeInitializationException:'System.Data.Entity.Internal.AppConfig' 的类型初始化程序引发了异常。')
    • 你可以看到这个thread
    • 嗨@Beginner,你能接受它作为答案吗?
    • 天哪,对不起,我明白了