dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.8k stars 3.2k forks source link

Microsoft.EntityFrameworkCore.SQLite on Android 7+ not working #8922

Closed tipa closed 2 years ago

tipa commented 7 years ago

I was successfully using Microsoft.EntityFrameworkCore.SQLite v.1.1.2 in a Xamarin.Android project. But for Android 7+, the app crashes with unauthorized access to "/system/lib64/libsqlite.so" (as reported here).

So I updated to version v2.0.0-preview1-final as this should fix the issue. However, this results in other problems like the following where the app compiled, but crashed while deploying/starting on the emulator:

...
06-21 09:58:18.970 D/Mono    ( 3632): Assembly Loader probing location: 'System.Runtime.CompilerServices.Unsafe'.
06-21 09:58:18.970 F/monodroid-assembly( 3632): Could not load assembly 'System.Runtime.CompilerServices.Unsafe' during startup registration.
06-21 09:58:18.970 F/monodroid-assembly( 3632): This might be due to an invalid debug installation.

I then tried to add the 'System.Runtime.CompilerServices.Unsafe' NuGet package manually to my project, but it didnt fix the problem. I messed around a lot, trying different build parameters and such and actually got the app running on an Android 7 emulator. However, after a project clean and rebuild, the error showed up again.

Any help on how to get EFCore Sqlite to work on Android 7?

Steps to reproduce

Sample project: https://github.com/tipa/EFCoreSample (Basically just the basic Android template from VS + the Nuget preview package)

Further technical details

EF Core version: v2.0.0-preview1-final Database Provider: Microsoft.EntityFrameworkCore.Sqlite Xamarin: 4.5.0.486 Xamarin.Android: 7.3.1.2 Operating system: Windows 10 IDE: Visual Studio 2017 15.2

tipa commented 7 years ago

Any comments/updates on this?

bricelam commented 7 years ago

Have you tried again with EF Core version 2.0.0 and Xamarin.Android 7.5?

tipa commented 7 years ago

How can I test it with Xamarin.Android 7.5? Version 7.4 was just released a few days ago

erikpowa commented 7 years ago

@tipa Xamarin Updater Extension for VS 2017 (market). For older version of VS you can switch channels in vs->options->xamarin (not sure if alpha or beta). I had the same error so I updated everything to the latest version, VS + NET Core (2.0+) + Xamarin Preview (I'm using Xamarin.Android 7.5.0.3 ), and everything is fine since then.

tipa commented 7 years ago

@erikpowa that means I am forced to use VS17 Preview in order to use Xamarin.Android 7.5, is that correct? I would probably even do that if that finally resolves that issue.

Edit: well, downloaded VS17 preview + Xamarin.Android 7.5, however they still seem to be very beta, I encounter several issues which make it impossible for me to even compile the app.

NPadrutt commented 7 years ago

I have the same issue on the current stable and on the preview of Xamarin. But I still have the same issue..

bricelam commented 7 years ago

(Marking for re-triage. Xamarin supports .NET Standard 2.0 now, but this still seems to be blocked.)

groege commented 7 years ago

Hi, this is very serious for us, since we are already shipping the Android app with netstandard1.4. For now just no one use a Android 7+ phone, but now we might loose many important customers if we can't get it working very soon. Can you give me any advice on how to handle this? Is there any workaround to make it work on Android 7+? I'd be grateful for any help. Thanks.

NPadrutt commented 7 years ago

If you target 6.0 with your app and use EF 1.x it should work on Android 7+ as well. There is a message shown sometimes when you start, but you can just hit ok and it should be functional.

groege commented 7 years ago

Thank you! Didn't think there was an easy solution like this (though i also added this: https://github.com/aspnet/EntityFrameworkCore/issues/7870). For now this is the better solution anyway until we can upload UWP projects to the app store with netcore2.0 ;)

NPadrutt commented 7 years ago

ah right. that one is also an important one :) Glad it worked :)

tipa commented 7 years ago

Any updates on this? Same problem exists with Xamarin.Android 8.0 and VS17 Update 4.

NPadrutt commented 7 years ago

It seems the issue only appears with either project.json or package ref. With the old packages.config it seems to work so far. I have two projects attached, one with packages.config (App1) und eines mit packagesRef (App2).

Desktop.zip

mkuennek commented 7 years ago

I have the same problem, an update would be much appreciated

alexdrl commented 7 years ago

We're having the same issue using EF Core 2.0.1 in VS 15.4.3 and VS 15.5 Preview 3 with no luck.

This is annoying because the Xamarin application is now blocked because of this.

erikpowa commented 7 years ago

@alexdrl

  1. use package.config and do a Clean Build or
  2. if you're using PackageReference, make sure you've manually added System.Runtime.CompilerServices.Unsafe 4.3.0 nuget package and do a Clean Build (delete obj/bin folders, restore nuget packages etc)
tipa commented 7 years ago

Thanks - while both of your suggestions work, they feel like a workaround. Would be awesome if we wouldnt have to use them in the future

alexdrl commented 7 years ago

@erikpowa We are in the point 2, using PackageReference. I cannot install System.Runtime.CompilerServices.Unsafe 4.3.0 because EF Core 2.0.0 uses the 4.4.0 version, so NuGet detects a package downgrade, and does not let me install 4.3.0.

bytefish commented 7 years ago

I investigated this a little. It seems like Microsoft.Extensions.Primitives has a dependency on System.Runtime.CompilerServices.Unsafe. When the Linker runs, it looks for the DLL in the directory of Microsoft.Extensions.Primitives, which is not downloaded by NuGet.

I have downloaded the NuGet Package System.Runtime.CompilerServices.Unsafe 4.4.0, extracted the DLL and linked it with a normal Reference. This way it will be copied into the output directories and can be linked.

This is my previous workaround:

@alexdrl I think the following trick is necessary to make it work.

Manually edit the csproj file of your project and define, that the NU1605 warning shouldn't be treated as an error:

<PropertyGroup>
  <NoWarn>$(NoWarn);NU1605</NoWarn>
</PropertyGroup>

Then in your Project File add the System.Runtime.CompilerServices.Unsafe Package in Version 4.3.0 as mentioned by @erikpowa:

<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.3.0" />

Then I would:

  1. Remove the App from the Device / Simulator
  2. Clean the NuGet Caches
  3. Clean all bin and obj folders
  4. Restore the Packages
  5. Build

After a successful build your Xamarin App should start with Entity Framework 2.0. 👍

alexdrl commented 7 years ago

@bytefish @erikpowa That workaround worked good... as we're working closely to the latest version of .NET Core/Standard and Xamarin, could you give us some explanation of why is this happening?

Thank you for the workaround.

Also, as we have found with other package version mismatch, this package downgrade workaround breaks the assemblies linking.

NPadrutt commented 6 years ago

When I add the System.Runtime.CompilerServices.Unsafe package I get an Exception during linking:

1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(2153,3): warning MSB3277: Found conflicts between different versions of "System.Runtime.CompilerServices.Unsafe" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1696,5): error MSB4018: The "LinkAssemblies" task failed unexpectedly.
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1696,5): error MSB4018: Mono.Linker.MarkException: Error processing method: 'System.Void Microsoft.Extensions.Primitives.InplaceStringBuilder::Append(System.String,System.Int32,System.Int32)' in assembly: 'Microsoft.Extensions.Primitives.dll' ---> Mono.Cecil.ResolutionException: Failed to resolve System.Void System.Runtime.CompilerServices.Unsafe::CopyBlockUnaligned(System.Void*,System.Void*,System.UInt32)
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1696,5): error MSB4018:    at Mono.Linker.Steps.MarkStep.HandleUnresolvedMethod(MethodReference reference)
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1696,5): error MSB4018:    at Mono.Linker.Steps.MarkStep.MarkMethod(MethodReference reference)
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1696,5): error MSB4018:    at Mono.Linker.Steps.MarkStep.MarkInstruction(Instruction instruction)
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1696,5): error MSB4018:    at Mono.Linker.Steps.MarkStep.MarkMethodBody(MethodBody body)
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1696,5): error MSB4018:    at Mono.Linker.Steps.MarkStep.ProcessMethod(MethodDefinition method)
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1696,5): error MSB4018:    at Mono.Linker.Steps.MarkStep.ProcessQueue()
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1696,5): error MSB4018:    --- End of inner exception stack trace ---
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1696,5): error MSB4018:    at Mono.Linker.Steps.MarkStep.ProcessQueue()
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1696,5): error MSB4018:    at Mono.Linker.Steps.MarkStep.ProcessEntireQueue()
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1696,5): error MSB4018:    at Mono.Linker.Steps.MarkStep.Process()
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1696,5): error MSB4018:    at Mono.Linker.Steps.MarkStep.Process(LinkContext context)
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1696,5): error MSB4018:    at Mono.Linker.Pipeline.Process(LinkContext context)
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1696,5): error MSB4018:    at MonoDroid.Tuner.Linker.Process(LinkerOptions options, LinkContext& context)
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1696,5): error MSB4018:    at Xamarin.Android.Tasks.LinkAssemblies.Execute(DirectoryAssemblyResolver res)
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1696,5): error MSB4018:    at Xamarin.Android.Tasks.LinkAssemblies.Execute()
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1696,5): error MSB4018:    at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1696,5): error MSB4018:    at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__26.MoveNext()

So I have to set the linker to "None" instead of "SDK Assemblies only". Is there a work around for that? I already set the entry mentioned by @bytefish .

This happens on VS15.5.

NokautBR commented 6 years ago

Could not load assembly 'System.Runtime.CompilerServices.Unsafe' during startup registration.

I have same issues when start a solution using Prism Template Pack (set only to Android), then i add Realm DB from Nuget packages. Without Realm, no issues I'm using Visual Studio 2017 with update 15.4 on Windows 10

almirvuk commented 6 years ago

@bytefish @erikpowa That workaround worked! Thanks!

tipa commented 6 years ago

@bytefish I included the linker.xml file with the suggested content, but no luck. This issue is now 6 months old and still we are using half baked workarounds that get broken with ever other VS or Xamarin release. Really frustrating...

bricelam commented 6 years ago

@divega Can we follow up with the Xamarin Android team on this?

divega commented 6 years ago

Thanks @bricelam. I started an email thread with the Xamarin folks to figure out with them how to best manage this and other issues that were reported here but appear to be Xamarin issues.

At least for now, I would like to make sure such issues are also marked area-external to distinguish them from any Xamarin-related issues we need to address on our side. Can you please help me make sure they are?

softlion commented 6 years ago

@bytefish fix works but requires the linker set to "Don't link". This is not good.

ConX-Ryan commented 6 years ago

This is still an issue? is this being worked on or should i move to a more stable library for a new project?

ghost commented 6 years ago

I had to move to Dapper because of this problem which works great on mobile but doesn't do migrations.

dazinator commented 6 years ago

I was using EF Core 2.0 just fine, and then issue only started occuring for me after I moved from packages.config to packagereference. @bytefish Adding a package reference to System.Runtime.CompilerServices.Unsafe 4.3.0 resulted in a package downgrade build error. I added the NoWarn for that error which allowed the build to proceed - which now gets as far as this linker error:

2>  C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1696,5): error MSB4018: The "LinkAssemblies" task failed unexpectedly.
2>C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1696,5): error MSB4018: Mono.Linker.MarkException: Error processing method: 'System.Void Microsoft.Extensions.Primitives.InplaceStringBuilder::Append(System.String,System.Int32,System.Int32)' in assembly: 'Microsoft.Extensions.Primitives.dll' ---> Mono.Cecil.ResolutionException: Failed to resolve System.Void System.Runtime.CompilerServices.Unsafe::CopyBlockUnaligned(System.Void*,System.Void*,System.UInt32)

Linker set to SdkOnly

Any ideas?

JonDouglas commented 6 years ago

We are tracking this issue in the following github issues:

https://github.com/xamarin/xamarin-android/issues/1154 https://github.com/xamarin/xamarin-android/issues/1196

ddobrev commented 6 years ago

Using packages.config instead of package references works around the problem.

dfoulk commented 6 years ago

Hey guys, not that it helps- but we had 3+ devs in our company alone run into this issue. So, for what it's worth, I don't think this is an isolated problem.

Latest Xamarin.Forms (.NET Standard) project + EntityFrameworkCore == runtime exception that only shows when deploying to an emulator.

Thanks, -Derek

ddobrev commented 6 years ago

@dfoulk it's even wider since it affects far more than EntityFrameworkCore. However, the workaround I've described does the job. For more details see https://github.com/xamarin/xamarin-android/issues/1196#issuecomment-358292869 and below.

dfoulk commented 6 years ago

@ddobrev - Yes, thank you for that. We're converting these projects to package.config- but a few errors seem to be preventing us from deploying still...

After following the instructions you provided in another thread:

Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCompat.Light.Dialog'.

No resource found that matches the given name: attr 'windowActionModeOverlay'.

styles.xml has apparently ran into some issues as a result :(

Update:

Something was wrong with our initial conversion. We reverted and tried again and all ended up okay... Thanks again!

ddobrev commented 6 years ago

@dfoulk you're welcome. About that error, your conversion was just fine - it's just that Xamarin.Android is notoriously poor at rebuilding. Every time a major restructuring, such as yours, takes place, the only way to properly rebuild everything is after deleting your bin/ and obj/ - that is, neither rebuild nor clean + build work in such cases.

jnormen commented 6 years ago

Any news here? I cannot go back to 1.3 :( And really need EF with SQLite to work on our app in Android. How is this long delay of a fix even possible? :(

dazinator commented 6 years ago

I am using it in our monodroid 8 app - it is working.. I had to convert the project to using packages.xml instead of packagereference though.. I'm waiting for this issue to be fixes so that I can convert back to packagereference and drop packages.xml again..

jnormen commented 6 years ago

yes, it's the package reference scenario I want them to fix. It has been many converts back and forth already, because of other libs and I'm done spending time converting, my clients do not pay for that job :(

divega commented 6 years ago

Clearing milestone to decide in triage whether we want to keep this for tracking purposes or whether we should close.

The underlying issues in Xamarin Android are now tracked in xamarin/xamarin-android#1154 and xamarin/xamarin-android#1196.

dfoulk commented 6 years ago

Thanks for the update @divega