e_sqlite3 library not found in Xamarin.Mac
Been trying to get EFCore with SQLite working when using Xamarin.Mac. At runtime it cannot load the e_sqlite3 library.
When switching to Xamarin.Modern everything works right away (but is not an option).
First thought it had to do with https://github.com/ericsink/SQLitePCL.raw/issues/343, but tried those work arounds and have had no luck.
I've got a sample demonstrating the issue with all the info I could gather here: https://github.com/nvanlaerebeke/EFCore_XamarinMac
In the sample above I made sure to use:
SQLitePCL.raw.SetProvider(new SQLitePCL.SQLite3Provider_e_sqlite3());
SQLitePCL.raw.FreezeProvider();
And only reference Microsoft.Data.Sqlite.Core with SQLitePCLRaw.core, SQLitePCLRaw.lib.e_sqlite3 and SQLitePCLRaw.provider.e_sqlite3 as mentioned in https://github.com/ericsink/SQLitePCL.raw/issues/343
Don't think I missed any: https://github.com/nvanlaerebeke/EFCore_XamarinMac/blob/master/XamarinMac_EFCore/packages.config
Any idea how to get it to work? Think this is an issue with Xamarin.Mac specifically as it works fine using Xamarin.Modern
This is a known problem, although, strangely, there appears to be no issue for it.
It was mentioned in the 2.0 release notes:
https://github.com/ericsink/SQLitePCL.raw/blob/master/v2.md
I'm guessing there is no workaround?, I'll try to switch to Xamarin.Modern and convert all the projects to .net standard
Actually I'm quite sure there IS a workaround, I just don't remember how to do it. :-)
The problem is conceptually rather simple -- we just need the e_sqlite3 library to end up where the Mac code can find it. Once that happens, everywhere works.
I'll take a closer look again and see if I can figure something out.
Would be much appreciated, can't imagine I'm the only one using Xamarin.Mac :)
"can't imagine I'm the only one using Xamarin.Mac"
Probably not, but it's close. :-) I've been surprised how few people have mentioned this problem.
I had similar problem with compilation in 'Release' and used following workaround. I added it into the project file:
<ItemGroup Condition=" '$(Configuration)' == 'Release'">
<NativeReference Include="/Users/runner/.nuget/packages/sqlitepclraw.lib.e_sqlite3/2.0.3/runtimes/osx-x64/native/libe_sqlite3.dylib">
<Kind>Dynamic</Kind>
<SmartLink>False</SmartLink>
</NativeReference>
</ItemGroup>
The following workaround worked for us:
<ItemGroup>
<PackageReference Include="SQLitePCLRaw.lib.e_sqlite3" Version="2.0.4" PrivateAssets="None" GeneratePathProperty="true" />
<NativeReference Include="$(PkgSQLitePCLRaw_lib_e_sqlite3)/runtimes/osx-x64/native/libe_sqlite3.dylib">
<Kind>Dynamic</Kind>
<SmartLink>False</SmartLink>
</NativeReference>
</ItemGroup>
The only way I could get this to work on my end is by copying the libe_sqlite3.dylib to my Xamarin.Mac projects Resources folder and then adding it as a native reference from there.
<ItemGroup>
<BundleResource Include="Resources\libe_sqlite3.dylib" />
</ItemGroup>
<ItemGroup>
<NativeReference Include="Resources\libe_sqlite3.dylib">
<Kind>Dynamic</Kind>
<SmartLink>False</SmartLink>
</NativeReference>
</ItemGroup>
You just need add the base sqlite3 lib:
<PackageReference Include="SQLitePCLRaw.lib.e_sqlite3" Version="2.1.2" />
<PackageReference Include="SQLitePCLRaw.lib.e_sqlcipher" Version="2.1.2" />
It can fix the DllNotFoundException error.