Does package microsoft.entityframeworkcore.sqlserver work with xamarin.forms?

Kurt K 116 Reputation points
2020-11-27T23:04:26.477+00:00

I am getting an error when trying to connect. I am only testing with the xamarin.ios project. I realize that connecting to a sql database over the internet is not a good idea. However, in my case the application connects only to a local database on a company network using a wifi connection.

Error when running with the ios simulator:

System.TypeInitializationException: 'The type initializer for 'Microsoft.Data.SqlClient.TdsParser' threw an exception.'

2020-11-27 13:54:11.795508-0800 TmMobile.iOS[22564:1043002]
Unhandled Exception:
System.TypeInitializationException: The type initializer for 'Microsoft.Data.SqlClient.SNILoadHandle' threw an exception. ---> System.DllNotFoundException: sni.dll assembly:<unknown assembly> type:<unknown type> member:(null)
at (wrapper managed-to-native) Microsoft.Data.SqlClient.SNINativeMethodWrapper.SNIInitialize(intptr)
at Microsoft.Data.SqlClient.SNINativeMethodWrapper.SNIInitialize () [0x00000] in H:\tsaagent4_work\2\s\src\Microsoft.Data.SqlClient
etcore\src\Interop\SNINativeMethodWrapper.Windows.cs:289
...

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,273 questions
Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
689 questions
0 comments No comments
{count} vote

Accepted answer
  1. Anthony Soto 81 Reputation points
    2021-01-08T19:19:11.203+00:00

    Hi Kurt,

    Unfortunately, this scenario is unsupported for Xamarin.Forms -> Xamarin.iOS. For Mobile platforms, the best practice (as mentioned in this thread) is to create an intermediate service to then connect to your database. Connecting directly through a mobile platform is heavily discouraged, so you will not find many resources or much supportability for this scenario.

    The Microsoft.Data.SqlClient.SNI.dll assembly is a Windows only assembly (Native networking implementation), which does not apply to the iOS platform. The DLL is used by Windows targeted SqlClient binaries (runtimes\win in NuGet package), so when implementation is on a non-Windows platform, the assembly is going to throw a DLLNotFound Exception when trying to use native Windows SNI.

    The SNI runtime binary dependency from the SqlClient package is attempting to target the native SNI assembly. There is the possibility of using Managed Networking by setting this AppContext switch in app code, but testing this proved to not have any effect on the missing SNI assembly error.
    Source: https://learn.microsoft.com/en-us/sql/connect/ado-net/appcontext-switches?view=sql-server-ver15#enabling-managed-networking-on-windows

    Aside from the issues with SNI when on the emulator, the error message being received on the physical device build is due to a Xamarin.iOS limitation with NetStandard. Xamarin.iOS must use AOT compilation, due to the lack of support for dynamic code generation on the iOS platform. This is why we are seeing AOT mentioned in the error, but regardless, there is a dependency on "System.Configuration.dll" from the SQLServer package and its dependencies which is not supported in the Xamarin.iOS .NET API profile.
    Source: https://learn.microsoft.com/en-us/xamarin/ios/internals/limitations#net-api-limitations

    Lastly, there is an open GitHub issue on the scenario you are experiencing located here: https://github.com/dotnet/SqlClient/issues/861
    While the scenario on iOS through Xamarin.Forms is deemed unsupported, the SQLClient side is investigating possible workarounds and further insight onto why the SNI assembly is being called on non-Windows platforms. Any updates from them on this scenario will likely be posted on that issue post - please feel free to interact with the post yourself as well to gather attention to it.

    Thank you,
    Anthony

    1 person found this answer helpful.

3 additional answers

Sort by: Most helpful
  1. JessieZhang-MSFT 7,701 Reputation points Microsoft Vendor
    2020-11-30T07:51:04.437+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    Entity Framework Core (EF Core) is a lightweight, extensible, cross-platform version of the Entity Framework. It’s slimmed down from the full Entity Framework and .NET Standard compatible.

    For more details, I think you can first read through the article Entity Framework Core with Xamarin.Forms

    and the official document : https://learn.microsoft.com/en-us/ef/core/

    Note: EF Core 5.0 will not run on .NET Standard 2.0 platforms, including .NET Framework.

    For more, you can check: https://devblogs.microsoft.com/dotnet/announcing-entity-framework-core-ef-core-5-rc2/

    Best Regards,

    Jessie Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Karen Payne MVP 35,016 Reputation points
    2020-12-05T23:28:52.447+00:00

    Hello,

    A better idea is to not connect directly to the database but instead use restful api that works with json data that can be deserialized to a instance of a class/model. The Xamarin platform ships with support for JSON out of the box.

    Here is a good place to start learning how to interact via rest service/api
    https://learn.microsoft.com/en-us/xamarin/cross-platform/data-cloud/web-services/

    Bottom line is to do EF work on a server, not on a cross platform project so the EF work is alway from the client side code.


  3. deckelmouck 191 Reputation points
    2021-01-11T10:43:23.25+00:00

    hello,

    had some similar issues. i wanted to connect directly to sqlserver within xamarin forms. it is no advise and on internet you should use a webservice for security reasons.

    here is my solution:

    best-practice-connecting-directly-to-sqlserver.html

    perhaps you can use this information and get a solution you'll need for your purpose.

    Regards