Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query: Connection is not closed after executing a query including a collection #6581

Closed
ajcvickers opened this issue Sep 21, 2016 · 2 comments
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@ajcvickers
Copy link
Contributor

This might be by design, but it surprised me so I am filing a bug on it. Running the following simple query results in the connection remaining open after ToList has completed:

DbConnection dbConnection;

using (var context = new MyContext())
{
    dbConnection = context.Database.GetDbConnection();

    Console.WriteLine(dbConnection.State);

    context.Parents.Include(u => u.Children).ToList();
    //context.Children.Include(u => u.Parent).ToList();

    Console.WriteLine(dbConnection.State);
}

Console.WriteLine(dbConnection.State);

Output:

Closed
Open
Closed

The connection is closed after the context is disposed. Including a reference does not leave the connection open.

Full repro with model:

public class MyContext : DbContext
{
    public DbSet<Parent> Parents { get; set; }
    public DbSet<Child> Children { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 
        => optionsBuilder.UseSqlServer(
        "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=inc-test-db; Integrated Security=True;");
}

public class Program
{
    public static void Main(string[] args)
    {
        using (var context = new MyContext())
        {
            context.Database.EnsureDeleted();
            context.Database.EnsureCreated();

            context.AddRange(
                new Parent { Children = new List<Child> { new Child(), new Child() } },
                new Parent { Children = new List<Child> { new Child(), new Child() } });

            context.SaveChanges();
        }

        DbConnection dbConnection;

        using (var context = new MyContext())
        {
            dbConnection = context.Database.GetDbConnection();

            Console.WriteLine(dbConnection.State);

            context.Parents.Include(u => u.Children).ToList();
            //context.Children.Include(u => u.Parent).ToList();

            Console.WriteLine(dbConnection.State);
        }

        Console.WriteLine(dbConnection.State);
    }
}

public class Parent
{
    public int Id { get; set; }
    public ICollection<Child> Children { get; set; }
}

public class Child
{
    public int Id { get; set; }
    public Parent Parent { get; set; }
}
@rowanmiller rowanmiller added this to the 1.0.2 milestone Sep 21, 2016
@smitpatel smitpatel added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Sep 26, 2016
@divega
Copy link
Contributor

divega commented Oct 12, 2016

Reopening to go through proper approval process.

@Eilon
Copy link
Member

Eilon commented Nov 1, 2016

This patch is approved, please ensure it is merged into the correct branch and building as part of the patch train.

@divega divega changed the title Query including a collection doesn't close the connection Query: Including a collection doesn't close the connection Dec 12, 2016
@divega divega changed the title Query: Including a collection doesn't close the connection Query: connection is not closed after executing a query including a collection Dec 13, 2016
@divega divega changed the title Query: connection is not closed after executing a query including a collection Query: Connection is not closed after executing a query including a collection Dec 13, 2016
cazangabriel added a commit to MountainWarehouse/EasyMWS that referenced this issue May 4, 2018
dotnet/efcore#6581
dotnet/efcore#7810

whenever this GetDbConnection() would be called it should also be disposed of. therefore this way of setting the connection string is not valid.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

5 participants