Skip to content

Compiled models generate non-aot compatible code (repro included) #36817

Closed

Description

@connorivy

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

EfAotTestProject.zip

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

  1. roji commented on Sep 18, 2025

    @roji
    Member

    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.

  2. GunboatDiplomat commented on Sep 18, 2025

    @GunboatDiplomat

    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.

  3. connorivy commented on Sep 18, 2025

    @connorivy
    Author

    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

    /// <summary>
    /// Creates a default <see cref="ValueComparer{T}" /> for the given type.
    /// </summary>
    /// <param name="type">The type.</param>
    /// <param name="favorStructuralComparisons">
    /// If <see langword="true" />, then EF will use <see cref="IStructuralEquatable" /> if the type
    /// implements it. This is usually used when byte arrays act as keys.
    /// </param>
    /// <returns>The <see cref="ValueComparer{T}" />.</returns>
    public static ValueComparer CreateDefault(
    [DynamicallyAccessedMembers(
    DynamicallyAccessedMemberTypes.PublicMethods
    | DynamicallyAccessedMemberTypes.PublicProperties)]
    Type type,
    bool favorStructuralComparisons)
    => (ValueComparer)CreateDefaultMethod.MakeGenericMethod(type).Invoke(null, [favorStructuralComparisons])!;

    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

  4. roji commented on Sep 18, 2025

    @roji
    Member

    @GunboatDiplomat I don't think our docs can be clearer:

    NativeAOT and query precompilation are highly experimental feature, and are not yet suited for production use. The support described below should be viewed as infrastructure towards the final feature, which will be released in a future version. We encourage you to experiment with the current support and report on your experiences, but recommend against deploying EF NativeAOT applications in production. See below for specific known limitations.

    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.

  5. changed the title [-]Compiled models generate non-aot compatible code (repo included)[/-] [+]Compiled models generate non-aot compatible code (repro included)[/+] on Sep 22, 2025
  6. added theissue type on Sep 24, 2025
  7. added this to the 11.0.0 milestone on Sep 24, 2025
  8. irunjic commented on Oct 1, 2025

    @irunjic
  9. AndriySvyryd commented on Oct 1, 2025

    @AndriySvyryd
  10. sylveon commented on Mar 21, 2026

    @sylveon

    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

    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 == null path.

  11. sylveon commented on Apr 11, 2026

    @sylveon

    This seems to also affect creating an index on a Guid column. The stack trace goes back to GetKeyValueComparer too.

  12. added 7 commits that reference this issue on Jun 17, 2026
    5d46860
    63ec34e
    ea371de
    f2fd387
    02b6079
    d87a3f1
    5dc20ee
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions