Skip to content
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

Attempting to use same navigation for two many-to-many relationships should result in last wins #29535

Open
Mr-Technician opened this issue Nov 10, 2022 · 7 comments

Comments

@Mr-Technician
Copy link

File a bug

Include your code

Adding an entity with a custom join table for its many to many relationship causes a NullReferenceException in this method of EF Core: https://github.com/dotnet/efcore/blob/main/src/EFCore/ChangeTracking/Internal/NavigationFixer.cs#L750

I have included a minimal reproduction: https://github.com/Mr-Technician/EFCoreNullReferenceException

The error occurs in any 6.0.x version and is working in 5.0.x. Note that my actual project uses SQL Server, but the error appears regardless of database provider.

Include stack traces

Object reference not set to an instance of an object.
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NavigationFixer.InitialFixup(InternalEntityEntry entry, Boolean fromQuery)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NavigationFixer.StateChanged(InternalEntityEntry entry, EntityState oldState, Boolean fromQuery)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntryNotifier.StateChanged(InternalEntityEntry entry, EntityState oldState, Boolean fromQuery)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.FireStateChanged(EntityState oldState)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState oldState, EntityState newState, Boolean acceptChanges, Boolean modifyProperties)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityGraphAttacher.PaintAction(EntityEntryGraphNode`1 node)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityEntryGraphIterator.TraverseGraph[TState](EntityEntryGraphNode`1 node, Func`2 handleNode)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityGraphAttacher.AttachGraph(InternalEntityEntry rootEntry, EntityState targetState, EntityState storeGeneratedWithKeySetTargetState, Boolean forceStateWhenUnknownKey)
   at Microsoft.EntityFrameworkCore.DbContext.SetEntityState(InternalEntityEntry entry, EntityState entityState)
   at Microsoft.EntityFrameworkCore.DbContext.SetEntityState[TEntity](TEntity entity, EntityState entityState)
   at EFCoreNullReferenceException.Program.Main(String[] args) in C:\Users\rnielsen\source\repos\EFCoreNullReferenceException\EFCoreNullReferenceException\Program.cs:line 37

Include provider and version information

EF Core version: 6.0.0x
Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer)
Target framework: (e.g. .NET 6.0)
Operating system: Windows
IDE: Visual Studio 2022 17.4

@milen-denev
Copy link

I have the same exception throwned but since .NET 7

@Mr-Technician
Copy link
Author

@milen-denev Was your code working .NET 6 and now no longer works in .NET 7? Or does it not run in either .NET 6 or .NET 7

@milen-denev
Copy link

milen-denev commented Nov 14, 2022

@milen-denev Was your code working .NET 6 and now no longer works in .NET 7? Or does it not run in either .NET 6 or .NET 7

Sorry for my late reply. I was fixing this exact issue for in-production application. This that I thing happen, some dependensies where in Version 6 (ex. Microsoft.EntityFrameworkCore.SqlServer) and other where in version 7 and this broked many futures like Include() or in general any type of quering.

After I rolled back to .NET 6 and then again to .NET 7 (because it stopped working in both versions for this exact reason) and changed all dependensies from 6 to 7 back to 6 and again to 7 it got fixed.

Of course this is only assumption. But it's the most logical (to me) reason for this happening.

It may also be the same reason for you as well. Take a carefull look to the dependensies version of anything Microsoft.EntityFrameworkCore.* .

@Mr-Technician
Copy link
Author

It may also be the same reason for you as well. Take a carefull look to the dependensies version of anything Microsoft.EntityFrameworkCore.* .

Unfortunately I don't think that is the case: https://github.com/Mr-Technician/EFCoreNullReferenceException/blob/master/EFCoreNullReferenceException/EFCoreNullReferenceException.csproj

@ajcvickers
Copy link
Member

@Mr-Technician The User.Calls property:

public virtual ICollection<Call> Calls { get; set; } //we don't really need this property but it makes the many-to-many mapping happy

is used for more than one many-to-many relationship. This is not supported. In EF7, this property is not required and can be removed. If EF Core 6, the usual workaround is to use two private collection properties.

@AndriySvyryd We should probably catch this in model validation.

@Mr-Technician
Copy link
Author

Adding a second collection fixed the issue. This project may stay on .NET 6 until the next LTS release next year, so having a non .NET7 workaround is good.

I agree though, this should be caught in model validation if possible.

@ajcvickers ajcvickers added closed-no-further-action The issue is closed and no further action is planned. and removed closed-no-further-action The issue is closed and no further action is planned. labels Nov 16, 2022
@ajcvickers ajcvickers changed the title NullReferenceException in NavigationFixer when adding entity with many-to-many joins Attempting to use same navigation for two many-to-many relationships should result in last wins Nov 19, 2022
@ajcvickers
Copy link
Member

Note from triage: the first relationship should end up as unidirectional through introduction of a shadow navigation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants