SqlClient: Can't create SqlParameter in EF Core 3.1.2 in Xamarin.Android application

I have Android Xamarin.Forms project. It depends on Db project with EF Core code (SQL Server) and targeted .Net Standard 2.0. Both projects in one solution.

With EF Core 2.2.6 everything works fine. When I switch to EF Core 3.1.2, the project does not work anymore.

I made the following changes to compile it:

  • I added Microsoft.Data.SqlClient 1.1.1 nuget project reference and use namespace Microsoft.Data.SqlClient instead of System.Data.SqlClient
  • Replace DbSet<Entitiy>.FromSql calls with DbSet<Entitiy>.FromSqlRaw

When I create SqlParameter and try to access it I get an error.

E.g. when I run the following code:

try
{
    var sqlParam = new SqlParameter("aaa", "bbb");
    var value = sqlParam.Value;
} catch (Exception ex)
{
    Debug.WriteLine($"Message: {ex.Message}");
    Debug.WriteLine($"StackTrace: {ex.StackTrace}");
}

I get the following error messages:

Message: Object reference not set to an instance of an object
StackTrace: at Microsoft.Data.SqlClient.SqlParameter.get_Value () [0x00000] in E:\SqlClientInternal\agent-1_work\5\s\src\Microsoft.Data.SqlClient etcore\ref\Microsoft.Data.SqlClient.cs:1222 at <MyProjectMember> [0x00012] in <MyProjectFile>.cs:100

When the sqlParam just assigned, and I want to view its value, I get the following view in Visual Studio:

enter image description here

I use Visual Studio 2019, I tried 16.3.4 and the latest as for now 16.4.6.

How to fix this issue? What is wrong with my code?

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Comments: 18 (8 by maintainers)

Most upvoted comments

@cheenamalhotra Is this issue dead? I’m getting an exception “Microsoft.Data.SqlClient is not supported on this platform.” when I try to upgrade from ef Core 2.x to ef Core 3.x or higher and build and run the MonoAndroid project.

Hi @sergtk

Please take a look at porting-cheat-sheet as there are a few more related namespaces that need update for applicable classes.