Hayden Ridgeway
Hayden Ridgeway

Reputation: 22

Unity "PlatformNotSupportedException" Error with Microsoft.Data.SqlClient

My goal is to be able to use the Microsoft.Data.SqlClient libraries in my scripts. I imported this library from the NuGet plugin in Unity. When I attempt to run code from this library in my scripts, I get the error:

PlatformNotSupportedException: Microsoft.Data.SqlClient is not supported on this platform.
Microsoft.Data.SqlClient.SqlConnection..ctor(system.string connectionString)

It errors on this line:

SqlConnection con = new SqlConnection(_connectionString);

Despite the many things I tried, I still cannot get around this error.

Project details:

Other possible important error notes:

Unity reports an error:

Errors during XML parse:
0x00007ff6876bb51d (Unity) StackWalker::GetCurrentCallstack
0x00007ff6876c05c9 (Unity) StackWalker::ShowCallstack
0x00007ff6886b0051 (Unity) GetStacktrace
0x00007ff688d6a23e (Unity) DebugStringToFile
0x00007ff6865aa9f8 (Unity) DebugLogHandler_CUSTOM_Internal_Log
0x0000016f61f74c03 (Mono JIT Code) (wrapper managed-to-native) UnityEngine.DebugLogHandler:Internal_Log (UnityEngine.LogType,UnityEngine.LogOption,string,UnityEngine.Object) 0x0000016f61f74b3b (Mono JIT Code) UnityEngine.DebugLogHandler:LogFormat (UnityEngine.LogType,UnityEngine.Object,string,object[])
0x0000016f61f748c0 (Mono JIT Code) UnityEngine.Logger:Log (UnityEngine.LogType,object)
0x0000016fc2817da8 (Mono JIT Code) UnityEngine.Debug:LogWarning (object)
0x0000016fcb5098eb (Mono JIT Code) UnityEditor.Android.AndroidSDKTools:DetectErrorsAndWarnings (string,string,string[])
0x0000016f4ef3c133 (Mono JIT Code) UnityEditor.Android.AndroidSDKTools:RunAndroidSdkTool (string,string,bool,string,string,string[])
0x0000016f4ef3936b (Mono JIT Code) UnityEditor.Android.AndroidSDKTools:ListComponentsVersions ()
0x0000016f4ef38c6b (Mono JIT Code) UnityEditor.Android.SDKManager:UpdatePackagesList ()
0x0000016f4ef3884b (Mono JIT Code) UnityEditor.Android.SDKManager:ListAvailable (UnityEditor.Android.SDKManager/Component)
0x0000016f4ef3821b (Mono JIT Code) UnityEditor.Android.PlayerSettingsEditorExtension/<>c__DisplayClass132_0:b__0 () 0x0000016f45fb6e26 (Mono JIT Code) System.Threading.ThreadHelper:ThreadStart_Context (object) 0x0000016f45fb63fe (Mono JIT Code) System.Threading.ExecutionContext:RunInternal (System.Threading.ExecutionContext,System.Threading.ContextCallback,object,bool) 0x0000016f45fb5e6b (Mono JIT Code) System.Threading.ExecutionContext:Run (System.Threading.ExecutionContext,System.Threading.ContextCallback,object,bool) 0x0000016f45fb6c2b (Mono JIT Code) System.Threading.ExecutionContext:Run (System.Threading.ExecutionContext,System.Threading.ContextCallback,object) 0x0000016f45fb6793 (Mono JIT Code) System.Threading.ThreadHelper:ThreadStart () 0x0000016f45fb6656 (Mono JIT Code) (wrapper runtime-invoke) object:runtime_invoke_void__this__ (object,intptr,intptr,intptr) 0x00007ffa43614c2e (mono-2.0-bdwgc) mono_jit_runtime_invoke (at C:/build/output/Unity-Technologies/mono/mono/mini/mini-runtime.c:3445) 0x00007ffa4354d254 (mono-2.0-bdwgc) do_runtime_invoke (at C:/build/output/Unity-Technologies/mono/mono/metadata/object.c:3068) 0x00007ffa43550b3d (mono-2.0-bdwgc) mono_runtime_delegate_try_invoke (at C:/build/output/Unity-Technologies/mono/mono/metadata/object.c:4452) 0x00007ffa4357fa2d (mono-2.0-bdwgc) start_wrapper_internal (at C:/build/output/Unity-Technologies/mono/mono/metadata/threads.c:1296) 0x00007ffa4357fbee (mono-2.0-bdwgc) start_wrapper (at C:/build/output/Unity-Technologies/mono/mono/metadata/threads.c:1348) 0x00007ffaef067374 (KERNEL32) BaseThreadInitThunk 0x00007ffaf051cc91 (ntdll) RtlUserThreadStart

Unity also reported many "Import Error Code(4)" when initially installing the packages.

Example message:

Message: Build asset version error: assets/packages/microsoft.identity.client.4.61.3/lib/netstandard2.0/microsoft.identity.client.dll.meta in SourceAssetDB has modification time of '2025-01-02T19:10:19.6060966Z' while content on disk has modification time of '2025-01-02T19:10:22.8671146Z'

Sources have told me to ignore these errors but since the library isn't working, there might be more to this.

Solution attempt 1: Edit the .csproj file

I saw that some solutions came from adding a package reference into the .csproj file.

What Data.SqlClient can be used with .net 8

Unfortunately, seems that unity reloads the .csproj file each time it builds. Unless I wasn't looking at the correct file, this wasn't working.

Solution attempt 2: add dlls to the to the Assets>Packages folder

I did two approaches for this. The first one I copied all the related folders from the main packages folder into the Assets>Packages folder and the second one was to download the NuGet packages, unzip the contents and add to the Assets>Package folder while making sure it was the same exact version as the one downloaded in the unity NuGet plugin. (5.2.2). Neither worked.

I did notice that the Microsoft.Data.SqlClient.6.0.0-preview.24331.3 kept appearing in the main packages folder despite I never installing that version. I assume this version is a beta version and will most likely want to use a different release that may be more stable.

Solution attempt 3: API compatibility level

Some solutions recommended changing the API compatibility level from .NET standard 2.1 to .NET standard 4.x or just .NET Framework. After setting all the different platforms' compatibility level in the Project Settings > Player, the error still persists.

I might have tried other solutions but I can't remember exactly. If one of the attempts above SHOULD work, then I may have done it wrong and I can retry them with some additional guidance if possible.

Upvotes: 0

Views: 77

Answers (1)

Pratik Lad
Pratik Lad

Reputation: 8402

Agreed with @Lex Li, the PlatformNotSupportedException error occurs because the Microsoft.Data.SqlClient library is not fully supported on all platforms or runtime environments that Unity uses. This is because Microsoft.Data.SqlClient relies on certain .NET Core or .NET Framework features that may not be available in Unity's runtime environment.

Unity typically runs on a subset of .NET (Mono or IL2CPP), and this subset does not always support the libraries or features required by Microsoft.Data.SqlClient.

To resolve this, Use System.Data.SqlClient Instead Microsoft.Data.SqlClient. System.Data.SqlClient is the predecessor of Microsoft.Data.SqlClient. It is sometimes more compatible with Unity's runtime.

Upvotes: 1

Related Questions