dotnet / dotnet/efcore

DbContext.ChangeTracker.Entries() throws StackOverflowException

Open
#29,740 7 comments 0 reactions 0 assignees View on GitHub
area-change-tracking customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

Calling `DbContext.ChangeTracker.Entries()` throws StackOverflowException after some removing some entities from navigation property collections.

### Include your code

```C#
var dbContext = DbContextFactory.CreateDbContext();
var product = await dbContext.ČíselníkVýrobky
.AsSplitQuery()
.Include(e => e.MaterialItems).ThenInclude(e => e.Material)
.FirstOrDefaultAsync(e => e.Kód == productCode, cancellationToken);

//Following method is required. If skipped, the exception in NOT thrown.
product .MaterialItems.Remove(product.MaterialItems[0]); //if this is skipped, exception is not thrown

foreach (var entry in dbContext.ChangeTracker.Entries()) //throws StackOverflowException
{
//never executed
}
```

### Include stack traces

Exception: `StackOverflowException: Exception_WasThrown` without StackTrace

Callstack from visual studio

```
System.Private.CoreLib.dll!System.RuntimeTypeHandle.GetGenericTypeDefinition(System.RuntimeType type)
System.Private.CoreLib.dll!System.RuntimeType.GetGenericTypeDefinition()
Microsoft.EntityFrameworkCore.dll!System.SharedTypeExtensions.IsNullableValueType(System.Type type)
Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetProperty(Microsoft.EntityFrameworkCore.Metadata.IPropertyBase propertyBase, object value, bool isMaterialization, bool setModified, bool isCascadeDelete, Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.CurrentValueType valueType)
Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.HandleConceptualNulls(bool sensitiveLoggingEnabled, bool force, bool isCascadeDelete)
Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetProperty(Microsoft.EntityFrameworkCore.Metadata.IPropertyBase propertyBase, object value, bool isMaterialization, bool setModified, bool isCascadeDelete, Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.CurrentValueType valueType)
[The 2 frame(s) above this were repeated 3840 times]
Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.HandleConceptualNulls(bool sensitiveLoggingEnabled, bool force, bool isCascadeDelete)
Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetProperty(Microsoft.EntityFrameworkCore.Metadata.IPropertyBase propertyBase, object value, bool isMaterialization, bool setModified, bool isCascadeDelete, Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.CurrentValueType valueType)
Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NavigationFixer.ConditionallyNullForeignKeyProperties(Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry dependentEntry, Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry principalEntry, Microsoft.EntityFrameworkCore.Metadata.IForeignKey foreignKey)
Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NavigationFixer.NavigationCollectionChanged(Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry entry, Microsoft.EntityFrameworkCore.Metadata.INavigationBase navigationBase, System.Collections.Generic.IEnumerable added, System.Collections.Generic.IEnumerable removed)
Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.ChangeTracking.Internal.ChangeDetector.DetectNavigationChange(Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry entry, Microsoft.EntityFrameworkCore.Metadata.INavigationBase navigationBase)
Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.ChangeTracking.Internal.ChangeDetector.LocalDetectChanges(Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry entry)
Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.ChangeTracking.Internal.ChangeDetector.DetectChanges(Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IStateManager stateManager)
Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.ChangeTracking.ChangeTracker.Entries()
```

### Include provider and version information

EF Core version: 6.0.11
Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer)
Target framework: (e.g. .NET 6.0)
Operating system: Window 11 Pro 21H2
IDE: Visual Studio 2022 17.3.3

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.