Repository navigation
Description
Bug description
I cannot get a small AOT sample project working with ef core.
I get many errors when publishing involving both the generated compiled model and the entityframeworkcore nuget packages and dependencies.
IL3050:
EfAotTestProject.CompiledModels.MyEntityEntityType.Create(RuntimeModel,RuntimeEntityType): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues overload or the GetValuesAsUnderlyingType method instead.
This was already brought up as an issue here #34057 but it is closed as completed. However, I think it should be reopened as I'm still getting the issue on 10.0 rc1 version of ef core.
IL2037:
Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ExecuteUpdateAsync(IQueryable1<TSource>,Action1<UpdateSettersBuilder`1>,CancellationToken): No members were resolved for 'ExecuteUpdate1(System.Linq.IQueryable{1},System.Collections.Generic.IReadOnlyList{ITuple})' on type 'Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions'.
The errors involving the ef packages are less specific. They look like this
/home/ubuntu/.nuget/packages/microsoft.entityframeworkcore/10.0.0-rc.1.25451.107/lib/net10.0/Microsoft.EntityFrameworkCore.dll : error IL2104: Assembly 'Microsoft.EntityFrameworkCore' produced trim warnings. For more information see https://aka.ms/il2104
The packages with trim warnings are
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.Relational
Microsoft.EntityFrameworkCore.Sqlite
I publish works if I try it a second time, but unsurprisingly I get missing code errors when I run the code
Your code
Stack traces
Unhandled exception. System.NotSupportedException: 'Microsoft.EntityFrameworkCore.ChangeTracking.ValueComparer.CreateDefault[System.Guid](System.Boolean)' is missing native code. MethodInfo.MakeGenericMethod() is not compatible with AOT compilation. Inspect and fix AOT related warnings that were generated when the app was published. For more information see https://aka.ms/nativeaot-compatibility
at System.Reflection.Runtime.MethodInfos.RuntimeNamedMethodInfo`1.GetUncachedMethodInvoker(RuntimeTypeInfo[], MemberInfo) + 0x2a
at System.Reflection.Runtime.MethodInfos.RuntimeMethodInfo.Invoke(Object, BindingFlags, Binder, Object[], CultureInfo) + 0x3f
at Microsoft.EntityFrameworkCore.ChangeTracking.ValueComparer.CreateDefault(Type type, Boolean favorStructuralComparisons) + 0xaa
at Microsoft.EntityFrameworkCore.Internal.NonCapturingLazyInitializer.EnsureInitialized[TParam,TValue](TValue&, TParam, Func`2) + 0x1d
at Microsoft.EntityFrameworkCore.Metadata.RuntimeProperty.<>c.<GetKeyValueComparer>b__52_0(RuntimeProperty property) + 0x31
at Microsoft.EntityFrameworkCore.Internal.NonCapturingLazyInitializer.EnsureInitialized[TParam,TValue](TValue&, TParam, Func`2) + 0x1d
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.SimplePrincipalKeyValueFactory`1..ctor(IKey) + 0x6e
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.KeyValueFactoryFactory.CreateSimpleNonNullableFactory[TKey](IKey key) + 0x31
at EfAotTestProject.CompiledModels.MyEntityEntityType.CreateAnnotations(RuntimeEntityType) + 0x1b9
at EfAotTestProject.CompiledModels.MyDbContextModel.Initialize() + 0x1e
at System.Threading.Thread.StartThread(IntPtr) + 0x131
at System.Threading.Thread.ThreadEntryPoint(IntPtr) + 0x19
Verbose output
EF Core version
10.0.0-rc.1.25451.107
Database provider
Microsoft.EntityFrameworkCore.Sqlite
Target framework
.NET 10.0 rc1
Operating system
Ubuntu 24.04.1 LTS (WSL)
IDE
VS Code
Activity
roji commentedon Sep 18, 2025
EF isn't yet fully NAOT-compatible, in the full sense of allowing a program to be published with zero warnings. However, in many cases the program will work correctly, despite the warnings. We hope to improve this situation in the future.
GunboatDiplomat commentedon Sep 18, 2025
What does this actually mean? Is EF usable with AOT or not? If even a simple app produces warnings, should we assume there’s a risk of unexpected runtime failures? Since EF with AOT has been promoted as a feature since version 8, it would be helpful to know whether you intend to fully support it, or if it has effectively been abandoned. If it’s not going to be supported, the documentation should be updated to clearly state that EF should not be used with AOT.
connorivy commentedon Sep 18, 2025
The program executes successfully when I change the entity's Id from a guid to an int. It looks like the guid value comparer ends up calling this code at some point
efcore/src/EFCore/ChangeTracking/ValueComparer.cs
Lines 249 to 264 in 0a52515
In the .net source, the MakeGenericMethod method is decorated with the 'RequiresDynamicCodeAttribute' while the ef source code does not have that attribute and instead is requiring public methods and properties to be preserved. The ef code probably should add the 'RequiresDynamicCodeAttribute' there and avoid using that method from compiled models
roji commentedon Sep 18, 2025
@GunboatDiplomat I don't think our docs can be clearer:
That said, if you're OK with using an experimental feature and have testing in place to ensure that your application works, you may choose to use it. NativeAOT/trimming warnings don't by themselves mean that your programs will fail.
[-]Compiled models generate non-aot compatible code (repo included)[/-][+]Compiled models generate non-aot compatible code (repro included)[/+]irunjic commentedon Oct 1, 2025
AndriySvyryd commentedon Oct 1, 2025
sylveon commentedon Mar 21, 2026
I am running into that exact issue. Even overriding the ValueComparer for all my Guid keys during model building and regenerating the compiled model does not appear to solve the issue - it still goes down the
_valueComparer == nullpath.sylveon commentedon Apr 11, 2026
This seems to also affect creating an index on a
Guidcolumn. The stack trace goes back toGetKeyValueComparertoo.Make type mapping generic so that it can create value comparers for N…
Make type mapping generic so that it can create value comparers for N…
Make type mapping generic so that it can create value comparers for N…
Make type mapping generic so that it can create value comparers for N…
Make type mapping generic so that it can create value comparers for N…
Make type mapping generic so that it can create value comparers for N…
Make type mapping generic so that it can create value comparers for N…