Skip to content

Instantly share code, notes, and snippets.

@slovely
Last active June 21, 2020 10:43
Show Gist options
  • Save slovely/17fda7d7f6524d3c40eb6fafcc44da6c to your computer and use it in GitHub Desktop.
Save slovely/17fda7d7f6524d3c40eb6fafcc44da6c to your computer and use it in GitHub Desktop.
EF Core StackOverflowException
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
namespace StackOverflowTest
{
public class Program
{
public static string ConnectionString =
"Data Source=localhost;Initial Catalog=EfCoreTest;Persist Security Info=True;User ID=**USERID***;Password=***PASSWORD***;MultipleActiveResultSets=True";
static void Main(string[] args)
{
SetupDatabase();
Console.WriteLine("EFCore");
Console.WriteLine("======");
using var context = new TestContext();
// Disable the change tracker and this works
//context.ChangeTracker.AutoDetectChangesEnabled = false;
var r = context.Recursives
.First(x => x.ParentId == null);
Console.WriteLine("Should be 1: " + r.Name);
Console.WriteLine(r.Children.Count);
Console.WriteLine("Should be 0: " + r.Children.First().Name);
}
private static void SetupDatabase()
{
using var context = new TestContext();
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
var recursive= new Recursive();
recursive.Children.Add(new Recursive());
context.Add(recursive);
context.SaveChanges();
}
}
public class TestContext : DbContext
{
public DbSet<Recursive> Recursives { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseLazyLoadingProxies();
optionsBuilder.UseSqlServer(Program.ConnectionString);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Recursive>(entity =>
{
entity.ToTable("Recursive");
entity.HasKey(x => x.Id);
entity.HasOne(x => x.Parent)
.WithMany(x => x.Children)
.HasForeignKey(x => x.ParentId)
;
});
}
}
public class Recursive
{
public Recursive()
{
Children = new List<Recursive>();
}
public virtual ICollection<Recursive> Children { get; set; }
public virtual Recursive Parent { get; set; }
public Guid? ParentId { get; set; }
public Guid Id { get; set; }
public string Name
{
get => Children.Count.ToString();
set{}
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="3.1.5" />
</ItemGroup>
</Project>
Recursive.get_Name() at C:\Projects\Git\EFCoreTesting\StackOverflowTest\Program.cs:line 80 [2]
[Lightweight Method Call]
ClrPropertyGetter<Recursive, string>.GetClrValue() [1]
InternalEntityEntry.ReadPropertyValue() [1]
InternalEntityEntry.get_Item() [1]
ChangeDetector.LocalDetectChanges() [1]
ChangeDetector.DetectChanges() [1]
ChangeDetector.DetectChanges() [1]
EntityEntry.DetectChanges() [1]
DbContext.TryDetectChanges() [1]
DbContext.Entry() [1]
LazyLoader.ShouldLoad() [1]
LazyLoader.Load() [1]
LazyLoadingInterceptor.Intercept() [1]
AbstractInvocation.Proceed() [1]
RecursiveProxy.get_Children() [1]
Recursive.get_Name() at C:\Projects\Git\EFCoreTesting\StackOverflowTest\Program.cs:line 80 [1]
[Lightweight Method Call]
new InternalEntityEntry.OriginalValues()
InternalEntityEntry.EnsureOriginalValues()
InternalEntityEntrySubscriber.SnapshotAndSubscribe()
StateManager.StartTrackingFromQuery()
QueryContext.StartTracking()
[Lightweight Method Call]
QueryingEnumerable<Recursive>.Enumerator.MoveNext()
Enumerable.Single<StackOverflowTest.Recursive>()
[Lightweight Method Call]
QueryCompiler.Execute<StackOverflowTest.Recursive>()
EntityQueryProvider.Execute<StackOverflowTest.Recursive>()
Queryable.First<StackOverflowTest.Recursive>()
Program.Main() at C:\Projects\Git\EFCoreTesting\StackOverflowTest\Program.cs:line 21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment