Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sergtk opened this issue Mar 13, 2020 · 18 comments
Open

Comments

@sergtk
Copy link

sergtk commented Mar 13, 2020

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 [0x00012] in .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?

@cheenamalhotra
Copy link
Member

Hi @sergtk

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

@sergtk
Copy link
Author

sergtk commented Mar 13, 2020

Hi @cheenamalhotra,

Thank you for the link.

When I try to replace namespace System.Data.SqlTypes with Microsoft.Data.SqlTypes I get an error:

error CS0234: The type or namespace name 'SqlTypes' does not exist in the namespace 'Microsoft.Data' (are you missing an assembly reference?)

This is in .NetStandard 2.0 project.
Nuget package Microsoft.Data.SqlClient is added to the project.

Should I add some other packages which are missed?

@cheenamalhotra
Copy link
Member

cheenamalhotra commented Mar 16, 2020

As mentioned in porting sheet, System.Data.SqlTypes namespace changes are applicable to only SqlFileStream.

Since SqlFileStream is only supported in .NET Core and .NET Framework (SqlFileSteam API Reference), you should not change this namespace for any other class in .NET Standard.

@sergtk
Copy link
Author

sergtk commented Mar 17, 2020

Thank you for clarification.

But it does not help.
In fact I can't execute any SQL query even without parameters.

E.g.

var db = new DataContext1()
var items = db.Items.FromSqlRaw($"SELECT * FROM [dbo].[Items]");
var itemsList = items.ToList();

The third line of code throws System.NullReferenceException exception.

@cheenamalhotra
Copy link
Member

cheenamalhotra commented Mar 17, 2020

Can I see your code if you could upload here in a small repro app?
I want to validate references you have in your project architecture and your code.

@sergtk
Copy link
Author

sergtk commented Mar 19, 2020

@cheenamalhotra I have uploaded 2 test projects at repo https://github.com/sergtk/AndroidXamarinEFCore

The first project is compiled with EF Core 2.2.6 and works without thrown exceptions: https://github.com/sergtk/AndroidXamarinEFCore/tree/master/AndroidXamarinEFCore226
The only thing is required is to provide correct user name, password, SQL server and database at file https://github.com/sergtk/AndroidXamarinEFCore/blob/master/AndroidXamarinEFCore226/AndroidXamarinEFCore/Db/App/Db/Config.cs

The second project is updated to EF Core 3.1.2 from the project above: https://github.com/sergtk/AndroidXamarinEFCore/tree/master/AndroidXamarinEFCore312
The error described in this ticket is reproduced in it.
It can be compiled and run. The only thing I did before uploading it to github I removed sensitive information in the file https://github.com/sergtk/AndroidXamarinEFCore/blob/master/AndroidXamarinEFCore312/AndroidXamarinEFCore/Db/App/Db/Config.cs

The first exception is thrown at the following line even without correct user name and password:
https://github.com/sergtk/AndroidXamarinEFCore/blob/master/AndroidXamarinEFCore312/AndroidXamarinEFCore/AndroidXamarinEFCore/App1.xaml.cs#L42 (line 42 of the file)

The rest part of the function OnStart() also throws exceptions either when creating SqlParameter instance members after the creation of it, or after executing SQL Statement and accessing selected data. But this part already requires correct access to the database (user name, password, server, database to reproduce the exception thrown.

I didn't provide DB migrations to create a database, but it is quite simple to create the table just with compatible columns with the ones in the C# code.

@cheenamalhotra
Copy link
Member

cheenamalhotra commented Mar 19, 2020

Hi @sergtk

Thanks for the repro, however I'm getting exception "Microsoft.Data.SqlClient is not supported on this platform." when running EFCore312 project with emulator.

Did you manage to get around that issue?

@sergtk
Copy link
Author

sergtk commented Mar 19, 2020

Hi @cheenamalhotra

I'm getting exception "Microsoft.Data.SqlClient is not supported on this platform."

I didn't get such an error.
But I just rechecked https://www.nuget.org/packages/Microsoft.Data.SqlClient/1.1.1 and I don't see the support of MonoAndroid.
I don't know how the project compiled, I also reinstalled Visual Studio 16.4.6 in case of Visual Studio corrupt.

But since there is no support of MonoAndroid for Microsoft.Data.SqlClient on nuget page, then does this mean that I can't use it on Android?
Does this mean that I should use System.Data.SqlClient to get SQL Server support of EF Core 3.1 on Android?

Anyway the question is how can I use SQL Server with EF Core 3.1 with Android?

Thank you

@cheenamalhotra
Copy link
Member

cheenamalhotra commented Mar 19, 2020

We support .NET Standard 2.0 so any framework that implements that is indirectly supported.

Are you by chance running on Unix platform? As I see the DLL is rightly picked for Unix runtime.

image

While on Windows, the DLL seems to be picked from 'lib' folder and not 'runtime', which throws NotSupportedException.

image

Can I debug for Unix to use the right DLL?

@sergtk
Copy link
Author

sergtk commented Mar 19, 2020

I use Windows 10.
I don't use Unix for development.

Probably these paths are shown somewhere for Android which is Unix like, but I didn't do something concerning Unix manually.

When I go to definition of SqlParameter class in Visual Studio during debugging, I get to the windows opened for the following library:

C:\Users\sergdev\.nuget\packages\microsoft.data.sqlclient\2.0.0-preview1.20021.1\ref\netstandard2.0\Microsoft.Data.SqlClient.dll

@cheenamalhotra
Copy link
Member

C:\Users\sergdev.nuget\packages\microsoft.data.sqlclient\2.0.0-preview1.20021.1\ref\netstandard2.0\Microsoft.Data.SqlClient.dll

This is ref for Visual Studio Intellisene.

On runtime different DLLs are loaded. But I still get the same error, which is a different problem since I don't work on Xamarin/Mono everyday, I'm not an expert in that area. I ran the app with VS 2019 Preview (as it has most recent updates) - v16.6.0 and .NET Core SDK 3.1.2.

Could you try to simplify the repro and remove layers that are non-relevant?
That might help us better understand underlying problem.

@sergtk
Copy link
Author

sergtk commented Mar 19, 2020

In fact, the app I uploaded is Android Xamarin.Forms application created by Visual Studio.
I added only EF Core code to reproduce the issue, it is quite straightforward.
I need some time to simplify the code even more.

As for now, I want to make it clear what I need.
I want to get a working sample application for Android Xamarin.Forms with EF Core 3.0+ support for SQL Server.
And I can't get such a sample application working.

In other words, I can't make a sample working application for Android with EF Core 3.0+ with SQL Server.
I would be glad if you have such an example.

Just one more note.
I have no problems creating the working code I need, but with SQLite instead of SQL Server.
The issue I have looks like related to Microsoft.Data.SqlClient.

@cheenamalhotra
Copy link
Member

In other words, I can't make a sample working application for Android with EF Core 3.0+ with SQL Server. I would be glad if you have such an example.

I agree, I'm looking into it too.. for my own learning and satisfaction 😄

1 info I'll need is if you can provide me binlog for your repro, in your environment.. you can generate it by running msbuild /bl from solution folder.

@sergtk
Copy link
Author

sergtk commented Mar 20, 2020

Hi @cheenamalhotra,

I updated sandboxes, they are much simpler now https://github.com/sergtk/AndroidXamarinEFCore

I can't provide binlog yet, because I have some issues to build solution with msbuild, I need investigate this yet.

I hope that EF Core 3.1 really supports Android.Xamarin as stated at https://docs.microsoft.com/en-us/ef/core/platforms/ .

@borisdj
Copy link

borisdj commented Apr 13, 2021

Can .Net library that targets .Net Standard 2.1 and has dependency on nuget Microsoft.Data.SqlClient v2.1.2 (used for Sqlite conn) as well as on package Microsoft.EntityFramework v5.0.5, be installed into Android app project?
It seems to give error (issue linked above):

Severity Code Description Project File Line Suppression State Error Could not install package 'Microsoft.Data.SqlClient.SNI.runtime 2.1.1'. You are trying to install this package into a project that targets 'MonoAndroid,Version=v11.0', but the package does not contain any assembly references or content files that are compatible with that framework.

@rlgordey
Copy link

rlgordey commented 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.

@Kaur-Parminder
Copy link
Contributor

Hi @rlgordey Can you provide Repro? So I can look into it. Are you using Xamrin.Forms?

@rlgordey
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants