Entity Framework 4.0 - 底层提供程序在打开时失败 [英] Entity Framework 4.0 - The underlying provider failed on Open

查看:587
本文介绍了Entity Framework 4.0 - 底层提供程序在打开时失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个具有Entity Framework 4.0的Web应用程序。不幸的是,当大量用户访问应用程序时,EF会抛出一个错误
基础提供程序在打开时失败

We have a web application with Entity Framework 4.0. Unfortunately, when the large volume of users hit the application the EF throws an error The underlying provider failed on Open

代码片段:

//DAL
public IQueryable<EmployeeEntity> GetEmployeeDetail()
{
    DatabaseEntities ent = new DatabaseEntities(this._connectionString);
    IQueryable<EmployeeEntity> result = from employee in ent.EmployeeEntity
                                           select employee;

    return result;
}

请注意,上述代码返回IQuerable。

Please note the above code returns IQuerable.


  1. 以上模式是否有可能导致异常发生的错误?

  2. Entity Framework何时以及如何确定关闭/打开

  3. EF的连接池的最大数量是多少?如何设置连接池的最大数量是多少?

  4. 我们需要明确指定开启和关闭

  5. 以下代码是否是解决上述问题的最佳方式?

  1. Is anything wrong with above pattern that could cause the exception to occur?
  2. When and how does Entity Framework determine to close / open db connection and also how long to retain?
  3. On what scenario does above error occurs?
  4. What's the maximum number of connection pool for EF and how do we configure?
  5. Do we need to explicitely specify open and close
  6. Is code below a best way to resolve above issue?







public IQueryable<EmployeeEntity> GetEmployeeDetail()
{
    using (DatabaseEntities ent = new DatabaseEntities(this._connectionString))
    {
        IQueryable<EmployeeEntity> result = from employee in ent.EmployeeEntity 
                                            select employee;                     
        return result.ToList().AsQuerable();
    }
}


推荐答案

ToList()调用将导致查询立即在数据库上运行,并且不以任何方式过滤将返回数据库中的每个员工。这可能会导致你的性能问题。

The ToList() call will cause the query to run on the database immediately and as this is not filtered in any way will return every employee in your database. This is probably likely to cause you performance issues.

但是你不能删除这个在你的情况下,因为如果你返回 IQueryable 直接然后上下文将被处置的时候你尝试和获取结果。

However you can't remove this in your case because if you return the IQueryable directly then the context will be disposed by the time you try and fetch results.

您可以:


  • 更改其工作方式,当方法返回并返回查询而不调用 ToList()时, ent 然后,您可以在调用 ToList()之前进一步过滤 IQueryable

  • 在方法中调用 ToList(),但是首先过滤/限制查询(例如将一些参数传递到方法中以指定此)以减少从数据库返回的行数。

  • change the way it works so that the scope of ent does not end when the method returns and return the query without calling ToList(). You can then further filter the IQueryable before calling ToList().
  • call ToList() within the method but filter/limit the query first (e.g. pass some parameters into the method to specify this) to reduce the number of rows coming back from the database.

这篇关于Entity Framework 4.0 - 底层提供程序在打开时失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆