Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions test/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
<PackageVersion Include="OpenTelemetry.Exporter.InMemory" Version="$(OpenTelemetryExporterInMemoryVersion)" />
<PackageVersion Include="SQLitePCLRaw.provider.sqlite3" Version="$(SQLitePCLRawVersion)" />
<PackageVersion Include="SQLitePCLRaw.provider.winsqlite3" Version="$(SQLitePCLRawVersion)" />
<PackageVersion Include="Testcontainers.MsSql" Version="4.10.0" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
<AssemblyName>Microsoft.EntityFrameworkCore.AspNet.SqlServer.FunctionalTests</AssemblyName>
<RootNamespace>Microsoft.EntityFrameworkCore</RootNamespace>
<SkipTests Condition="'$(OS)' != 'Windows_NT' AND '$(Test__SqlServer__DefaultConnection)' == ''">True</SkipTests>
<ImplicitUsings>true</ImplicitUsings>
<DefineConstants Condition="$([MSBuild]::IsOSPlatform('OSX'))">$(DefineConstants);EXCLUDE_ON_MAC</DefineConstants>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<AssemblyName>Microsoft.EntityFrameworkCore.OData.FunctionalTests</AssemblyName>
<RootNamespace>Microsoft.EntityFrameworkCore</RootNamespace>
<Nullable>disable</Nullable>
<SkipTests Condition="'$(OS)' != 'Windows_NT' AND '$(Test__SqlServer__DefaultConnection)' == ''">True</SkipTests>
<ImplicitUsings>true</ImplicitUsings>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<AssemblyName>Microsoft.EntityFrameworkCore.SqlServer.FunctionalTests</AssemblyName>
<RootNamespace>Microsoft.EntityFrameworkCore</RootNamespace>
<PreserveCompilationContext>true</PreserveCompilationContext>
<SkipTests Condition="'$(OS)' != 'Windows_NT' AND '$(Test__SqlServer__DefaultConnection)' == ''">True</SkipTests>
<ImplicitUsings>true</ImplicitUsings>
</PropertyGroup>

Expand Down Expand Up @@ -77,5 +76,6 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" />
<PackageReference Include="Testcontainers.MsSql" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Configuration;
using Testcontainers.MsSql;

namespace Microsoft.EntityFrameworkCore.TestUtilities;

Expand All @@ -16,8 +17,46 @@ public static class TestEnvironment
.Build()
.GetSection("Test:SqlServer");

public static string DefaultConnection { get; } = Config["DefaultConnection"]
?? "Data Source=(localdb)\\MSSQLLocalDB;Database=master;Integrated Security=True;Connect Timeout=60;ConnectRetryCount=0";
private static readonly Lazy<string> _defaultConnection = new(InitializeConnectionString);

public static string DefaultConnection => _defaultConnection.Value;

private static MsSqlContainer? _container;

private static string InitializeConnectionString()
{
// If a connection string is specified (env var, config.json...), always use that.
var configured = Config["DefaultConnection"];
if (!string.IsNullOrEmpty(configured))
{
return configured;
}

// Otherwise, on Windows default to LocalDB, and on non-Windows spin up a testcontainer
if (OperatingSystem.IsWindows())
{
return "Data Source=(localdb)\\MSSQLLocalDB;Database=master;Integrated Security=True;Connect Timeout=60;ConnectRetryCount=0";
}

_container = new MsSqlBuilder("mcr.microsoft.com/mssql/server:2025-latest")
.Build();

_container.StartAsync().GetAwaiter().GetResult();

Comment on lines +41 to +45
Copy link

Copilot AI Feb 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If _container.StartAsync() throws (e.g. Docker not available, image pull failure), the container instance is left undisposed because the ProcessExit handler is registered only after the start completes. Wrap startup in a try/catch/finally (disposing the container on failure) and/or register cleanup before starting to avoid leaving orphaned containers behind.

Copilot uses AI. Check for mistakes.
AppDomain.CurrentDomain.ProcessExit += (_, _) =>
{
try
{
_container.DisposeAsync().AsTask().GetAwaiter().GetResult();
}
catch
{
// Ignore errors during container cleanup
}
};

return _container.GetConnectionString();
}

private static readonly string _dataSource = new SqlConnectionStringBuilder(DefaultConnection).DataSource;

Expand Down
2 changes: 1 addition & 1 deletion test/EFCore.SqlServer.FunctionalTests/config.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"Test": {
"SqlServer": {
"DefaultConnection": "Data Source=(localdb)\\MSSQLLocalDB;Database=master;Integrated Security=True;Connect Timeout=60;ConnectRetryCount=0",
"DefaultConnection": null,
"ElasticPoolName": "",
"SupportsMemoryOptimized": null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<AssemblyName>Microsoft.EntityFrameworkCore.SqlServer.HierarchyId.Tests</AssemblyName>
<RootNamespace>Microsoft.EntityFrameworkCore.SqlServer</RootNamespace>
<Nullable>disable</Nullable>
<SkipTests Condition="'$(OS)' != 'Windows_NT' AND '$(Test__SqlServer__DefaultConnection)' == ''">True</SkipTests>
<ImplicitUsings>true</ImplicitUsings>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<RootNamespace>Microsoft.EntityFrameworkCore</RootNamespace>
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
<AssemblyName>Microsoft.EntityFrameworkCore.VisualBasic.FunctionalTests</AssemblyName>
<SkipTests Condition="'$(OS)' != 'Windows_NT' AND '$(Test__SqlServer__DefaultConnection)' == ''">True</SkipTests>
<LangVersion>latest</LangVersion>
</PropertyGroup>

Expand Down
Loading