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

Open connection in tests if needed #30171

Merged
merged 1 commit into from
Feb 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Data;

namespace Microsoft.EntityFrameworkCore.TestUtilities;

public class RelationalQueryAsserter : QueryAsserter
Expand Down Expand Up @@ -65,19 +67,32 @@ private static (DbConnection, DbTransaction, int, int) ExecuteOurDbCommand(int e

private static int ExecuteReader(DbCommand command)
{
using var reader = command.ExecuteReader();
var needToOpen = command.Connection?.State == ConnectionState.Closed;
if (needToOpen)
{
command.Connection.Open();
}

// Not materializing objects here since automatic creation of objects does not
// work for some SQL types, such as geometry/geography
var count = 0;
if (reader.HasRows)

using (var reader = command.ExecuteReader())
{
while (reader.Read())
// Not materializing objects here since automatic creation of objects does not
// work for some SQL types, such as geometry/geography
if (reader.HasRows)
{
count++;
while (reader.Read())
{
count++;
}
}
}

if (needToOpen)
{
command.Connection.Close();
}

return count;
}
}