question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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?

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
rlgordeycommented, Sep 5, 2021

@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.

1reaction
cheenamalhotracommented, Mar 13, 2020

Hi @sergtk

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't create SqlParameter in EF Core 3.1.2 in Xamarin. ...
I have Android Xamarin.Forms project. It depends on Db project with EF Core code (SQL Server) and targeted .Net Standard 2.0.
Read more >
Xamarin Android application is not able to connect to MVC ...
The Issue I am facing is that the Android application is throwing an exception while connecting with MVC core web API. And here...
Read more >
Microsoft.Data.SqlClient 5.1.1
Provides the data provider for SQL Server. These classes provide access to versions of SQL Server and encapsulate database-specific protocols, ...
Read more >
7d0a9ec5cef6b3ca8fc0f5d94f5a...
In this tutorial, you create a Xamarin.Forms application that performs data access against a SQLite database using. Entity Framework Core.
Read more >
Entity Framework Documentation
Entity Framework (EF) Core is a lightweight and extensible version ... step-by-step through creating a simple application that queries and ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found