locked
ExecuteReader requires an open and available Connection. The connection is currently closed. RRS feed

  • Question

  • User-875744750 posted

    in my mvc4 web application, i have two Connexion string: one for remote MSSqlServer and other for local sqlExpress.

    i have used entity framwork version 6.1.3

    i have taken 3 entities Person, User that inheret from Persone and Structure

    i have used unitOfWork pattern:

    public class UniteOfWork : IDisposable
        {
            private bool disposing;        
            private ESCadreSupContext _context = new ESCadreSupContext();        
            private StructureBLL _structureBll;
            private UserBLL _userBll;
            
            
            public UniteOfWork()
            {
    
            }
    
            public PersonneBLL PersonneBLL 
            {
                get
                {
                    return _personneBll ?? new PersonneBLL(_context);
                } 
            }
    
            
    
            public StructureBLL StructureBLL 
            {
                get
                {
                    return _structureBll ?? new StructureBLL(_context);
                }
            }
    
            public UserBLL UserBLL
            {
                get
                {
                    return _userBll ?? new UserBLL(_context);
                }
            }
    
            public void Save()
            {
                this._context.SaveChanges();
            }
    
            
        }

    in the first, i have worked with the remot server connection string, after that, i have switched to sqlserverexpress connexion string for testing Adding personne with image to database

    after, i have returned to remot sql server connection string

    the following exceptions have been throwed

    An error occurred while executing the command definition. See the inner exception for details with innerexception:Internal connection fatal error. Error status: 15, Token: 0

    The underlying provider failed on Open. innerException

    The connection is not closed. The current state of the connection is being connected.

    when i put this code in unitOfWork class

    #region IDisposable Members 
    
    public void Dispose() 
    { 
        if (disposing) { 
            _context.Dispose(); 
        } 
    } 
    
    #endregion

    this exception has been  throwing

    and the Exeception "An error occurred while executing the command definition. See the inner exception for details." are throwining with the inner exception "ExecuteReader requires an open and available Connection. The connection is currently closed."

    why? what Error i have did?Should i use using bloc ? there are not other solution?

    the code that throw Exception in UserBLL class is the following (user entity inheret from personne entity)

    public List<User> GetFiltered(Expression<Func<User, bool>> filter)
    {
         return Context.Personnes.OfType<User>().Include(u => u.Roles).Where(filter).ToList();
    }
    

    Sunday, December 22, 2019 8:21 AM

All replies

  • User1120430333 posted

    in the first, i have worked with the remot server connection string, after that, i have switched to sqlserverexpress connexion string for testing Adding personne with image to database

    What are talking about that you switched back to another connectionstring? Is this being done in the continuous execution of code? Do you know that you even hit the Dispose()? If you're doing some kind of connectionstring swapping across database servers in the continuous execution of the same code, then that's highly problematic.

    Sunday, December 22, 2019 2:58 PM
  • User-875744750 posted

    Is this being done in the continuous execution of code?

    what do you mean by in the continuous execution of code?

    i have used a code first with migration so when i switch between two sql server, i ensure that : if a migration is added to one of them, i add it to another sql sever and update the databases in two sql server before debug, so i don't think that happened in  the continuous execution of code

    Monday, December 23, 2019 9:37 AM
  • User665608656 posted

    Hi bensam,

    The underlying provider failed on Open. innerException

    The connection is not closed. The current state of the connection is being connected.

    According to your description, I suggest you go back to the first question. Can you tell us the wrong sentence?

    This error message may caused by you use this object in two threads at the same time, it also may caused by you use it again before not close.

    I hope you can provide us with detailed Bll and underlying code.

    You can also refer to this link for more details:

    EF. The connection was not closed. The connection's current state is connecting

    Multithreading Entity Framework: The connection was not closed. The connection's current state is connecting

    Best Regards,

    YongQing.

    Tuesday, December 24, 2019 9:18 AM
  • User1120430333 posted

    DA924

    Is this being done in the continuous execution of code?

    what do you mean by in the continuous execution of code?

    i have used a code first with migration so when i switch between two sql server, i ensure that : if a migration is added to one of them, i add it to another sql sever and update the databases in two sql server before debug, so i don't think that happened in  the continuous execution of code

    Are you doing a migration? Just what is happening when the exception is thrown? What is the program doing?

    Tuesday, December 24, 2019 9:20 AM