Open IanRawley opened 5 days ago
How can I get more information in order to better narrow down the cause and produce a minimal reproduction?
Thanks for reporting this! What kind of information do you think you need? If you have a project on-hand which causes the exception, can you try stripping it down until you get a minimal repro which you can post? Or alternatively, you can try starting from scratch with an empty project, and add stuff to it until you manage to reproduce the error.
How can I get more information in order to better narrow down the cause and produce a minimal reproduction?
Thanks for reporting this! What kind of information do you think you need? If you have a project on-hand which causes the exception, can you try stripping it down until you get a minimal repro which you can post? Or alternatively, you can try starting from scratch with an empty project, and add stuff to it until you manage to reproduce the error.
The only way I can see an NRE happening here is if GetUnsafeAccessorName() is being called with a null MemberInfo. If I could figure out what entity was being processed I could narrow down what about it causes the problem, and then replicate it.
Unfortunately the existing solution is too big to pare down at this point, so I'm going to have to start with a basic model and start adding bits till it breaks.
@roji I finally figured it out and have been able to create a minimal reproduction. You can find it here: https://github.com/IanRawley/MinimalOptimizeNRERepo.git
The problem is this pattern on a model for what will be navigation collections:
private List<Post> _children = new();
public IReadOnlyCollection<Post> Posts => _children;
Specifically, if the name of the private backing field doesn't "match" the public facing name, optimize throws an NRE. If you change _children to _posts, or Posts to Children, the problem goes away. This only happens with the --nativeaot flag.
An addendum, it seems the problem is any backing field that doesn't match the public member name. It doesn't have to be a navigation collection.
I currently have an app using EF Core with the database functions spread across several projects:
With the release of EF Core 9, I'd like to precompile the model and queries with Native AOT, to hopefully boost performance on Android. Given the above separation of responsibilities, I felt it made the most sense to try and compile the model into another assembly, to be referenced as needed. However when trying to optimize the context from an effectively empty project (contains only an IDesignTimeDbContextFactory) the dotnet tools throw an NRE as seen below:
Trying a similar setup using the basic Blog/Post tutorial model generates without issue, so the problem seems to be something specific to this App's model rather than the splitting into multiple assemblies. How can I get more information in order to better narrow down the cause and produce a minimal reproduction?
EF Core version: 9.0.0 Database provider: Microsoft.EntityFrameworkCore.Sqlite Target framework: .NET 8 Operating system: Windows 11 IDE: Visual Studio 2022 17.12.1