-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
System.NullReferenceException thrown for many-to-many without navigation properties on one or both sides of the relationship #25383
Comments
Note for triage: Model created with EF Core 5.0.8 below. Note the skip-navigation for "Roles" that is not a shadow nav but also not mapped to any CLR field or property.
|
Notes from triage: this was not intended to work in 5.0, since there is no "Roles" property or field. This requires unidirectional skip navigations, which is tracked by #3864. However, we were not correctly catching this in model building, and it unfortunately worked accidentally for some scenarios. @JakeYal The workaround until #3864 is implemented is to add a private navigation property for the inverse. For example: public class Permission
{
public Guid Id { get; set; }
private ICollection<Role> Roles { get; set; } = null!;
} This allows EF to create the metadata correctly and should therefore work in all scenarios, rather than only where the incorrect metadata does not cause other errors. @AndriySvyryd I think you indicated in triage where this changed in 6.0, but I haven't been able to find it. Also, for this invalid case we should throw a better exception, potentially pointing to the workaround for people who have been using this pattern in 5.0. |
I think we didn't add a better exception because we planned to add support for this in 6.0 |
Note from triage: Putting this in 6.0 to throw with a better message. |
Duplicate of #23362 |
When creating a many-to-many mapping in EF Core 5, we did not need to specify any navigation properties on the entities making up the relationship. The below code works in EF Core 5.0.8:
This no longer works in EF Core 6.0 Preview 6. Trying to script a migration throws a NullReferenceException, and for existing code (I was migrating a NET 5.0 project to NET 6.0), this throws a NRE at runtime (with a mostly identical stack trace),
Reproduceable code example:
https://github.com/JakeYal/EFCore6ManyToManyRepro
To reproduce, just run
dotnet ef migrations add initial
dotnet-ef version:
Migration stack trace and verbose output
Runtime NullReferenceException stack trace
Provider and version information
EF Core version: 6.0.0-preview.6.21352.1
Database provider: Tested with Microsoft.EntityFrameworkCore.SqlServer and Microsoft.EntityFrameworkCore.Sqlite
Target framework: .NET 6.0
Operating system: Windows 10
IDE: Visual Studio 17.0.0 Preview 2.1
The text was updated successfully, but these errors were encountered: