Upgradin to .Net 10: System.PlatformNotSupportedException : Microsoft.Data.SqlClient is not supported on this platform

Summary

The System.PlatformNotSupportedException is thrown when running tests in Azure after upgrading a .NET 8 project to .NET 10 and updating all NuGet packages. This error occurs despite adding the Microsoft.EntityFrameworkCore.SqlServer NuGet package directly to the project.

Root Cause

The root cause of this issue is that Microsoft.Data.SqlClient is not supported on the platform being used in the Azure pipeline. This is likely due to the fact that the pipeline is configured to use a Linux environment, while Microsoft.Data.SqlClient is only supported on Windows.

Why This Happens in Real Systems

This issue can occur in real systems due to the following reasons:

  • Incompatible NuGet packages: Using NuGet packages that are not compatible with the target platform can cause this error.
  • Incorrect pipeline configuration: Configuring the pipeline to use an incorrect environment or platform can lead to this issue.
  • Missing dependencies: Failing to include required dependencies or packages can cause the System.PlatformNotSupportedException to be thrown.

Real-World Impact

The real-world impact of this issue includes:

  • Failed deployments: The error can cause deployments to fail, leading to downtime and lost productivity.
  • Delayed testing: The issue can delay testing and debugging, causing delays in the development cycle.
  • Increased costs: Resolving the issue can require additional resources and time, increasing costs.

Example or Code

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.SqlServer;

public class MyDbContext : DbContext
{
    public MyDbContext(DbContextOptions options) : base(options)
    {
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer("Server=myserver;Database=mydatabase;User Id=myuser;Password=mypassword;");
    }
}

How Senior Engineers Fix It

Senior engineers can fix this issue by:

  • Checking the pipeline configuration: Verifying that the pipeline is configured to use the correct environment and platform.
  • Updating NuGet packages: Ensuring that all NuGet packages are compatible with the target platform.
  • Including required dependencies: Adding any missing dependencies or packages required by the project.
  • Using platform-agnostic packages: Using packages that are supported on multiple platforms, such as Npgsql for PostgreSQL databases.

Why Juniors Miss It

Junior engineers may miss this issue due to:

  • Lack of experience: Limited experience with .NET and Azure pipelines can make it difficult to identify the root cause of the issue.
  • Insufficient knowledge of NuGet packages: Not understanding the compatibility and dependencies of NuGet packages can lead to errors.
  • Inadequate testing: Failing to thoroughly test the application on different platforms and environments can cause issues to go undetected.