实体框架4.1性能问题

时间:2011-10-24 18:52:45

标签: c# asp.net-mvc-3 entity-framework entity-framework-4.1 ef-code-first

我目前正在ASP.NET MVC 3项目中使用Entity Framework。 在循环浏览视图中的记录以显示它们时,我遇到了严重的性能问题。

数据正在快速接收,所以我知道这不是与我们的远程oracle服务器的连接,并且我正在使用的模型中没有延迟加载的关系,但每条记录需要120-300ms来处理一个简单的3带有动作链接的字段输出。

目前,加载具有800条记录的页面需要3分钟。

我尝试过使用配置选项进行调整,但似乎没有任何帮助。

任何人都有任何想法?

编辑:控制器代码

readonly OracleSampleManagerContext db = new OracleSampleManagerContext();

public virtual ActionResult Index()
{
        var spList = db.SamplePoints.OrderBy(e=>e.Id).ToList(); 
        return View(MVC.Reports.Views.SamplePointList, spList);
}

    <h2>
    Selection By Sample Point
</h2>

<table>
@foreach (var sp in Model)
{
        System.Diagnostics.Stopwatch sw = new  System.Diagnostics.Stopwatch();
        sw.Start();

    <tr>
        <td>@Html.ActionLink(sp.Id, MVC.Reports.Results(sp.Id))</td>
        <td>@sp.Description</td>
        <td>@sp.PointLocation</td>
        <td>@sw.ElapsedMilliseconds</td>
    </tr>

    sw.Stop();
    sw.Reset();
}

</table>

示例:

0200    72" Sewer to river - Once through cooling water     OUTFALLS    346ms
0400    66" Sewer to river - Combined effluent  OUTFALLS    347ms
0500    54" Sewer to river - Once through cooling water     OUTFALLS    388ms
06-AI-18    TBA in Water    IB2     228ms
06-AI-31    TBA in Water    IB2     172ms

1 个答案:

答案 0 :(得分:2)

我的猜测是MVC.Reports.Results(sp.Id)执行某种数据库查找,并且由于您在将模型发送到视图之前将其转换为列表,因此您现在必须再次为每个记录命中数据库。 800条记录需要801个单独的数据库,而不是一个。

  1. 从第一个查询中删除ToList()。
  2. 重构MVC.Reports.Results(sp.Id)取一个对象而不是一个int,并在该方法中直接处理该对象。
  3. 以上两种情况都可能要求您将实体上下文的范围从操作中移出到Controller。