question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

StackOverflowException generating query with many records of inlined data

See original GitHub issue

If a query has to import many records of inlined data, above a certain threshold, the generation of the query fails with a StackOverflowException within Entity Framework. The following minimal code sample reproduces this issue:

using System;
using System.Data.Entity;
using System.Linq;

namespace EntityFrameworkStackOverflow
{
	class Program
	{
		static void Main(string[] args)
		{
			var ids = Enumerable.Range(1, 5000).Select(n => Guid.NewGuid()).ToList();

			var query = new Context().Table.Join(ids, record => record.Id, id => id, (record, key) => record);

			query.ToString();
		}
	}

	public class Context : DbContext
	{
		public DbSet<Table> Table { get; set; }
	}

	public class Table
	{
		public Guid Id { get; set; }
	}
}

The exception occurs on the query.ToString() line, and occurs exactly the same way if instead the IQueryable<Table> is enumerated. The stack trace at the time of the exception looks like this:

EntityFramework.dll!System.Data.Entity.Core.Query.InternalTrees.BasicOpVisitor.VisitChildren(System.Data.Entity.Core.Query.InternalTrees.Node n)
EntityFramework.dll!System.Data.Entity.Core.Query.PlanCompiler.PropertyPushdownHelper.VisitSetOp(System.Data.Entity.Core.Query.InternalTrees.SetOp op, System.Data.Entity.Core.Query.InternalTrees.Node n)
EntityFramework.dll!System.Data.Entity.Core.Query.InternalTrees.BasicOpVisitor.Visit(System.Data.Entity.Core.Query.InternalTrees.UnionAllOp op, System.Data.Entity.Core.Query.InternalTrees.Node n)
EntityFramework.dll!System.Data.Entity.Core.Query.InternalTrees.UnionAllOp.Accept(System.Data.Entity.Core.Query.InternalTrees.BasicOpVisitor v, System.Data.Entity.Core.Query.InternalTrees.Node n)
EntityFramework.dll!System.Data.Entity.Core.Query.InternalTrees.BasicOpVisitor.VisitNode(System.Data.Entity.Core.Query.InternalTrees.Node n)
EntityFramework.dll!System.Data.Entity.Core.Query.InternalTrees.BasicOpVisitor.VisitChildren(System.Data.Entity.Core.Query.InternalTrees.Node n)
EntityFramework.dll!System.Data.Entity.Core.Query.PlanCompiler.PropertyPushdownHelper.VisitSetOp(System.Data.Entity.Core.Query.InternalTrees.SetOp op, System.Data.Entity.Core.Query.InternalTrees.Node n)
EntityFramework.dll!System.Data.Entity.Core.Query.InternalTrees.BasicOpVisitor.Visit(System.Data.Entity.Core.Query.InternalTrees.UnionAllOp op, System.Data.Entity.Core.Query.InternalTrees.Node n)
EntityFramework.dll!System.Data.Entity.Core.Query.InternalTrees.UnionAllOp.Accept(System.Data.Entity.Core.Query.InternalTrees.BasicOpVisitor v, System.Data.Entity.Core.Query.InternalTrees.Node n)
EntityFramework.dll!System.Data.Entity.Core.Query.InternalTrees.BasicOpVisitor.VisitNode(System.Data.Entity.Core.Query.InternalTrees.Node n)
EntityFramework.dll!System.Data.Entity.Core.Query.InternalTrees.BasicOpVisitor.VisitChildren(System.Data.Entity.Core.Query.InternalTrees.Node n)
EntityFramework.dll!System.Data.Entity.Core.Query.PlanCompiler.PropertyPushdownHelper.VisitSetOp(System.Data.Entity.Core.Query.InternalTrees.SetOp op, System.Data.Entity.Core.Query.InternalTrees.Node n)
EntityFramework.dll!System.Data.Entity.Core.Query.InternalTrees.BasicOpVisitor.Visit(System.Data.Entity.Core.Query.InternalTrees.UnionAllOp op, System.Data.Entity.Core.Query.InternalTrees.Node n)
EntityFramework.dll!System.Data.Entity.Core.Query.InternalTrees.UnionAllOp.Accept(System.Data.Entity.Core.Query.InternalTrees.BasicOpVisitor v, System.Data.Entity.Core.Query.InternalTrees.Node n)
....

(This repeated pattern of calls continues as far as Visual Studio is willing to follow stack frames.)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
ajcvickerscommented, Mar 18, 2019

EF Team Triage: This issue is not something that our team is planning to address in the EF6.x code base. This does not mean that we would not consider a community contribution to address this issue.

Moving forwards, our team will be fixing bugs, implementing small improvements, and accepting community contributions to the EF6.x code base. Larger feature work and innovation will happen in the EF Core code base (https://github.com/aspnet/EntityFramework).

Closing an issue in the EF6.x project does not exclude us addressing it in EF Core. In fact, a number of popular feature requests for EF have already been implemented in EF Core (alternate keys, batching in SaveChanges, etc.).

BTW this is a canned response and may have info or details that do not directly apply to this particular issue. While we’d like to spend the time to uniquely address every incoming issue, we get a lot traffic on the EF projects and that is not practical. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we use canned responses for common triage decisions.

0reactions
ajcvickerscommented, Mar 18, 2019

@logiclrd I could give it another try. However, reading through again I missed the large number of Ids in the list. At some point EF reaches a limit as to the number of items it can handle in a single list like this. We’re not planning to make any changes to EF6 in this respect.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - StackOverflowException in EntityFramework.dll when ...
I am facing a problem with entity framework. I am reciving an array of ints, and than I fetch these records from database...
Read more >
StackOverflowException Class (System)
The exception that is thrown when the execution stack exceeds the stack size. This class cannot be inherited.
Read more >
StackOverflowException in DataGridView - .NET Framework
DataGridView control that has many rows on a Tablet PC, the control crashes and generates a stack overflow exception.
Read more >
Is inline SQL still classed as bad practice now that we have ...
When you save the file, my extension generates c# wrapper classes, so you never write a line of connection code, or command code,...
Read more >
Beginning ASP.NET for Visual Studio 2015
SortExpression attribute, 326 for database columns, 329–330 sorting, 323,324–339 ... 56, 70, 85 SQL (Structured Query Language), 268, 321 queries and stored ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found