Skip to content

Commit

Permalink
Open connection in tests if needed (#30171)
Browse files Browse the repository at this point in the history
  • Loading branch information
roji authored Feb 4, 2023
1 parent be7ec87 commit 5045b40
Showing 1 changed file with 21 additions and 6 deletions.
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 void ExecuteTheirDbCommand(

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;
}
}

0 comments on commit 5045b40

Please sign in to comment.