C# 使用ASP.NET MVC和实体框架时出现异常

C# 使用ASP.NET MVC和实体框架时出现异常,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,ASP.NET MVC和实体框架非常新 我正试图建立一个包含三个表的数据库,当我运行代码并转到“Tickets”页面时,出现以下异常: 已存在与此命令关联的打开的DataReader,必须先关闭该命令 第一类 public class Ticket { [Key] public int ID { get; set; } public string Description { get; set; } [ForeignKey("Practice")] publ

ASP.NET MVC和实体框架非常新

我正试图建立一个包含三个表的数据库,当我运行代码并转到“Tickets”页面时,出现以下异常:

已存在与此命令关联的打开的DataReader,必须先关闭该命令

第一类

public class Ticket
{
    [Key]
    public int ID { get; set; }
    public string Description { get; set; }
    [ForeignKey("Practice")]
    public int PracticeID { get; set; }

    public string Contact { get; set; }
    public string Category { get; set; }

    //insert Support type using ViewBag (Support type listed in Models.Practices)
    [DataType(DataType.DateTime)]
    [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
    public DateTime Due { get; set; }

    [DataType(DataType.DateTime)]
    [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
    public DateTime TimeLogged { get; set; }

    [ForeignKey("Consultant")]
    public int ConsultantID { get; set; }
    public string ConsultantName { get; set; }
    public string Status { get; set; }

    public virtual Practice Practice { get; set; }
    public virtual Consultant Consultant { get; set; }
}
第2类:

public class Practice
{
    [Key]
    public int PracticeID { get; set; }
    [Display(Name = "Practice Name")]
    public string PracName { get; set; }
    [Display(Name = "Practice Number")]
    public int PracNumber { get; set; }
    public string Contact { get; set; }

    [Display(Name = "Support Type")]
    public string Support { get; set; }
    public string Tel { get; set; }
    public string Cell { get; set; }

    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }
    public string Address { get; set; }
    public virtual List<Ticket> Ticket { get; set; }
}
错误似乎来自索引中的HTML代码

@model IEnumerable<MAD.Models.Ticket>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>




<p>
    User: @Html.DropDownList("User", "All")
    Description: @Html.TextBox("SearchString") <br />
    <input type="submit" value="Search" />
</p>


<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Consultant.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Practice.PracName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Description)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Contact)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Category)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Due)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.TimeLogged)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Status)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Consultant.Name)--ERROR CODE
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Practice.PracName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Description)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Contact)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Category)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Due)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.TimeLogged)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Status)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
            @Html.ActionLink("Details", "Details", new { id=item.ID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.ID })
        </td>
    </tr>
}
</table>
@model IEnumerable
@{
ViewBag.Title=“Index”;
}
指数

@ActionLink(“新建”、“创建”)

用户:@Html.DropDownList(“用户”、“全部”) 描述:@Html.TextBox(“搜索字符串”)

@DisplayNameFor(model=>model.Consultant.Name) @DisplayNameFor(model=>model.Practice.PracName) @DisplayNameFor(model=>model.Description) @DisplayNameFor(model=>model.Contact) @DisplayNameFor(model=>model.Category) @DisplayNameFor(model=>model.Due) @DisplayNameFor(model=>model.timeloged) @DisplayNameFor(model=>model.Status) @foreach(模型中的var项目){ @DisplayFor(modelItem=>item.Consultant.Name)--错误代码 @DisplayFor(modelItem=>item.Practice.PracName) @DisplayFor(modelItem=>item.Description) @DisplayFor(modelItem=>item.Contact) @DisplayFor(modelItem=>item.Category) @DisplayFor(modelItem=>item.Due) @DisplayFor(modelItem=>item.TimeLoged) @DisplayFor(modelItem=>item.Status) @ActionLink(“编辑”,“编辑”,新的{id=item.id})| @ActionLink(“详细信息”,“详细信息”,新的{id=item.id})| @ActionLink(“删除”,“删除”,新的{id=item.id}) }
您已经从控制器中传入一个IQueryable作为您的模型,该模型在控制器方法中称为tickets。当您在模型上启动for each循环时,EF开始通过连接检索结果,但直到循环结束,它才真正完成使用该连接。导致错误的行正在尝试访问相关的Consultant属性,这会触发EF尝试从DB加载该属性,但这会导致异常,因为您仍在循环中,并且它仍在使用连接检索tickets查询

最简单的方法是强制EF在循环之前检索结果。我会将控制器中的最后一行更改为View(tickets.ToArray)或类似的内容


我不确定,但启用多个活动结果集也可能会解决此问题。

这很可能是因为您正在查询
db.Consultants
,然后是
db.Ticket
,在这两者之间,您的连接永远不会被处理。通常,查询是在其他dll或层上执行的。问题在于“使用”的范围用于在检索数据后进行处置
public ActionResult Index(string searchString, string Consultants)
{
        var UserLst = new List<string>();

        var UserQry = from d in db.Consultants
                      orderby d.Name
                      select d.Name;

        UserLst.AddRange(UserQry.Distinct());
        ViewBag.User = new SelectList(UserLst);

        var tickets = from m in db.Ticket
                      select m;

        if (!String.IsNullOrEmpty(searchString))
        {
            tickets = tickets.Where(s => s.Description.Contains(searchString));
        }

        if (!string.IsNullOrEmpty(Consultants))
        {
            tickets = tickets.Where(x => x.ConsultantName == Consultants);
        }

        return View(tickets);
    }
@model IEnumerable<MAD.Models.Ticket>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>




<p>
    User: @Html.DropDownList("User", "All")
    Description: @Html.TextBox("SearchString") <br />
    <input type="submit" value="Search" />
</p>


<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Consultant.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Practice.PracName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Description)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Contact)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Category)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Due)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.TimeLogged)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Status)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Consultant.Name)--ERROR CODE
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Practice.PracName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Description)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Contact)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Category)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Due)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.TimeLogged)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Status)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
            @Html.ActionLink("Details", "Details", new { id=item.ID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.ID })
        </td>
    </tr>
}
</table>