How to Fix ‘Microsoft.Data.SqlClient is not Supported on this Platform’. – Windows ASP.NET Core Hosting 2024 | Review and Comparison

How to Fix ‘Microsoft.Data.SqlClient is not Supported on this Platform’.

The following is the error message that you can find when deploy your database online:

Microsoft.Data.SqlClient: Microsoft.Data.SqlClient is not supported on this platform

Why You See this Error?

This error sometimes popped up after because dependency updates, merging feature branches. The code I had was something like this:

try
{
 var config = new ConfigurationBuilder()
  .SetBasePath(context.FunctionAppDirectory)
  .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
  .AddEnvironmentVariables()
  .Build();

 string connectionString = config["ConnectionStrings:ReadWriteConnection"];
 var optionsBuilder = new DbContextOptionsBuilder<ReadWriteApplicationDbContext>();
 optionsBuilder.UseSqlServer(connectionString);

 using (var appdb = new ReadWriteApplicationDbContext(optionsBuilder.Options))
 {
  _ = await appdb.Database.ExecuteSqlRawAsync("SELECT 1");
 }
}
catch (Exception e)
{
 return new ExceptionResult(e, true);
}

And running it locally failed every single time with an error along the lines of “Microsoft.Data.SqlClient: Microsoft.Data.SqlClient is not supported on this platform.“.

How to Fix the Error?

There’s plenty of different reasons that could cause this and I will help you to fix the error.

1. Downgrade your .NET or dependency version

You might not want to do this – however, if you’ve upgraded to .NET Core 3.x unintentionally, it might be a viable option for you to downgrade to .NET Core 2.2.x.

Clean & build, and see if it helps!

Actually, there’s plenty of ways you could fix your project by tweaking the versions of your Functions SDK or .NET Core runtime – or even your dependencies!

And that brings us to the next option…

2. Downgrading isn’t feasible? Make sure you’re on the latest version!

There seems to be quite a few cases, where simply updating your .NET Core, Microsoft.NET.Sdk.Functions AND any other dependencies that might internally reference Microsoft.Data.SqlClient (such as Entity Framework Core!)

3. Set your “Microsoft.NET.Sdk.Functions” dependency to a specific version

If you’re using .NET Core 3.x but your SDK version is <3.0.3, go ahead and upgrade it to 3.0.3 or newer!

Some users describe downgrading

Microsoft.NET.Sdk.Functions

from 3.0.3 (or newer) to 3.0.2 helps, too.

Clean & build (and maybe even nuke all temp & artifact folders) and see if it helps!

A lot of users report 3.0.7 to be functional. Fiddling with the versions didn’t help me, though.

4. Move your Database Context Creation to Startup.cs if it’s somewhere else

Visual Studio might mess up including the right dependencies for your function, if you’re only creating the DbContext somewhere else than Startup. Try instead configuring your context in the Startup.cs using SqlConnectionStringBuilder and see if it helps!

… or if you want to be a sly dog, keep your implementation as it is, and just add this to Startup.cs:

var sb = new SqlConnectionStringBuilder(); 
var cs = sb.ConnectionString;

5. Skip copying your function.deps file

Perhaps your function.deps.json file is getting misconstructed and causing the conflict, which will finally fail loading the required dependency.

Long story short, add this to your function .csproj file’s <PropertyGroup> :

<SkipFunctionsDepsCopy>true</SkipFunctionsDepsCopy>

Like shown below:

This was the one that fixed the issue for me!

6. New! Remove <SkipFunctionsDepsCopy> node from your .csproj -file

After an update to Microsoft.NET.Sdk.Functions, maybe you can see same error message again.

So, please fixed by removing <SkipFunctionsDepsCopy> node from my .csproj -file altogether!

Just clean and build, and you should be good!