Efcore: DbContext Not Being Disposed Ef Core 1.1

Created on 6 Dec 2016  路  10Comments  路  Source: dotnet/efcore

Steps to reproduce

using (var ctx = new ReportingDbContext())
{
     var result = await ctx.Companies.ToListAsync();
     return View(result);
}

The issue

The connection to the database is not being disposed after the result is being returned to the view. After the view has finished rendering, the view result is complete and nothing makes requests to the controller.

Further technical details

EF Core version: 1.1.0
Operating system: Windows 10
Visual Studio version: 2015 Version 14.0.25422.01 Update 3

Other details about my project setup:
I am using the following database
Microsoft SQL Server 2016 (RTM) - 13.0.1601.5 (X64) Apr 29 2016 23:23:58 Copyright (c) Microsoft Corporation Express Edition (64-bit) on Windows 10 Home 6.3 (Build 14393: )

This was addressed in #6581 and patch was approved. I do not believe it was merged into the correct and put into the patch train.

closed-could-not-reproduce

Most helpful comment

@ajcvickers I was able to add Pooling=False in my connection string and prove your position that Activity Monitor is not an accurate method due to connection pooling. Thank you for very much for the your time and help. I will close this issue due to it being a matter of user education.

All 10 comments

@euclid47 I am not sure if what you are seeing is related to the issue described in #6581, because that involved queries with Include(). Anyway, the fix for that issue is part of a patch release that we haven't shipped yet. By the way, as far as I understand #6581 manifests only when MARS is disabled, so you could try enabling MARS in the connection string as a workaround.

If it isn't related to #6581, we would probably need additional information to repro. A repro project would be good.

@divega
Here is another example from my project where the connection is not getting disposed.

 public async Task<IActionResult> Index()
        {

            var result = new List<CompanyListingVm>();
            var companies = stormpath.getUserCompanies(account, false);

            if (companies.Any())
            {
                using (var ctx = new ReportingPlusDbContext())
                {
                    foreach (var c in companies)
                    {
                        var tmp = await ctx.Companies.Include(x => x.CompanyAddress).FirstOrDefaultAsync(x => x.Id == c.Id);
                        var empCount = await ctx.Employees.Where(x => x.Active && x.CompanyId == c.Id).CountAsync();

                        result.Add(new CompanyListingVm
                        {
                            Company = (CompanyVm)tmp,
                            EmployeeCount = empCount
                        });
                    }
                }
            }

            return result.Any() ? View(result) : View();
        }

If I do not enable MARS I only have one connection that does not get disposed. If I enable MARS three connections do not get disposed. Enabling MARS is not a workaround and it actually increases the number of connections not disposed.

@smitpatel does this ring any bells?

This is not related to #6581 since there is no collection include.
@euclid47 - Can you provide a stand-alone repro code?
The query in first post is very simple query. If it is showing that behavior then probably there are other factors causing it. (Notice SqlServer 2016)

@smitpatel I will have to create a new project and push it up to github. The current project is company proprietary. The code will be as close as possible. It will take me a few days to get it pushed.

@divega @smitpatel
I have created a project that demonstrates the issue.
https://github.com/euclid47/ExampleNotDisposing

@euclid47 What mechanism are you using to determine that connections are not getting disposed?

@ajcvickers I am using the Activity Monitor in sql server management studio 2016

@euclid47 This is not an accurate way of determining that connections have been closed or disposed because the SQL Client ADO.NET provider is using connection pooling. This means that a connection can be closed and disposed in .NET without the underlying pipeline to the database being closed.

@ajcvickers I was able to add Pooling=False in my connection string and prove your position that Activity Monitor is not an accurate method due to connection pooling. Thank you for very much for the your time and help. I will close this issue due to it being a matter of user education.

Was this page helpful?
0 / 5 - 0 ratings