efcore icon indicating copy to clipboard operation
efcore copied to clipboard

When looking up methods via reflection, be trimming/AOT-friendly

Open roji opened this issue 3 years ago • 17 comments

  • [x] Stop using GetRequiredRuntimeMethod (see this comment)
  • [ ] When looking up generic methods, use the pattern described here (thanks @hez2010)

roji avatar Oct 08 '21 20:10 roji

I just got this exception when executing a self-contained, trimmed executable with EF Core 6.0.0:

System.TypeInitializationException: The type initializer for 'Microsoft.EntityFrameworkCore.SqlServer.Query.Internal.SqlServerMathTranslator' threw an exception.
---> System.InvalidOperationException: Could not find method 'Ceiling' on type 'System.Math'
at System.SharedTypeExtensions.GetRequiredRuntimeMethod(Type type, String name, Type[] parameters)
at Microsoft.EntityFrameworkCore.SqlServer.Query.Internal.SqlServerMathTranslator..cctor()

I quickly realized it was because of trimming and I had to disable it.

I see you explicitly mention GetRequiredRuntimeMethod which appears in this stack trace so I'm looking forward to a trimming friendly version of EF Core. 😉

0xced avatar Nov 18 '21 15:11 0xced

@0xced yeah, this is one of the high-priority items for EF7 - stay tuned, it'll hopefully make it into an early preview.

roji avatar Nov 18 '21 16:11 roji

We ran in this issue, too when switching to .NET6 / EF6 with trimmed binaries on production systems (Docker Images). Very annoying, now the builds can't be trimmed anymore 😢

seriouz avatar Dec 13 '21 21:12 seriouz

For a workaround with EF Core 6, see this (thanks @EjaYF).

roji avatar Dec 28 '21 21:12 roji

verified, workaround fixes the issue for my scenario, thanks @EjaYF, @roji. Will this be officially fixed in .net 6?

lechgu avatar Dec 31 '21 21:12 lechgu

This issue is in the 7.0.0 milestone. There's little chance we'll be fixing this in 6.0.0, but we'll discuss that after the fix is done and the general trimming situation is clearer.

roji avatar Dec 31 '21 22:12 roji

EF6 is LTS so people will use it until 8. November 2024. In my opinion the trimming fix should be made in EF6 when it will be supported for such a long time.

seriouz avatar Jan 02 '22 22:01 seriouz

@seriouz @lechgu @EjaYF if someone can share a minimal, trimmed console application which works with EF Core 5.0 (i.e. executes some trivial query), that would help push this forward. I'm seeing various linker-related failures when doing that, since EF Core 5.0 (and 6.0) weren't annotated for trimming in any way.

roji avatar Jan 02 '22 23:01 roji

Never mind, I've managed to get a trimmed version working here. I'll update soon.

roji avatar Jan 03 '22 10:01 roji

FYI I've merged #27098 for 6.0.2 - that's a temporary fix which specifically fixes the Math-related errors; in my testing it allowed at least basic queries to run properly in trimmed applications. This isn't a guarantee that everything will work - just an attempt to unblock people for basic usage. I'm tackling trimming in a more serious way for 7.0, please report any further problems you run across.

roji avatar Jan 06 '22 16:01 roji