When the Include method is called,use "_" as the variable name in the lambda expression , throws KeyNotFoundException.
```c#
Exception message:
KeyNotFoundException: The given key was not present in the dictionary.
Stack trace:
System.ThrowHelper.ThrowKeyNotFoundException()
System.Collections.Generic.Dictionary.get_Item(TKey key)
Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.DefaultQueryExpressionVisitor.GetParameterValue
lambda_method(Closure , AnonymousObject )
System.Linq.Lookup.CreateForJoin(IEnumerable
System.Linq.Enumerable+
System.Collections.Generic.EnumerableHelpers.ToArray
System.Linq.Buffer..ctor(IEnumerable
System.Linq.OrderedEnumerable+
System.Linq.Enumerable+SelectIPartitionIterator.MoveNext()
Microsoft.EntityFrameworkCore.Query.Internal.QueryBuffer.IncludeCollection
lambda_method(Closure , QueryContext , Ticket , Object[] )
Microsoft.EntityFrameworkCore.Query.Internal.IncludeCompiler._Include
System.Linq.Enumerable+SelectIPartitionIterator.MoveNext()
Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider+<_TrackEntities>d__17.MoveNext()
Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider+ExceptionInterceptor+EnumeratorExceptionInterceptor.MoveNext()
System.Linq.AsyncEnumerable+AsyncEnumerableAdapter.MoveNextCore(CancellationToken cancellationToken)
System.Linq.AsyncEnumerable+AsyncIterator+
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
System.Runtime.CompilerServices.ConfiguredTaskAwaitable+ConfiguredTaskAwaiter.GetResult()
System.Collections.Generic.AsyncEnumerableHelpers+
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
System.Runtime.CompilerServices.ConfiguredTaskAwaitable+ConfiguredTaskAwaiter.GetResult()
System.Collections.Generic.AsyncEnumerableHelpers+
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
System.Runtime.CompilerServices.TaskAwaiter.GetResult()
InMemoryDatabase.Controllers.ValuesController+
+
var result = await this._appDbContext.Set
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
System.Runtime.CompilerServices.TaskAwaiter.GetResult()
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker+
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker+
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker+
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker+
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Builder.RouterMiddleware+
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware+
### Steps to reproduce
```c#
namespace InMemoryDatabase
{
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Infrastructure.Internal;
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options)
: base(options)
{
}
public CoreOptionsExtension CoreOptions { get; set; }
public InMemoryOptionsExtension InMemoryOptions { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Ticket>().ToTable("Ticket").HasMany(_ => _.Items).WithOne(_ => _.Ticket)
.HasForeignKey(_ => _.TicketId).OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<TicketItem>().ToTable("TicketItem");
}
}
public class Ticket
{
public long Id { get; set; }
public IEnumerable<TicketItem> Items { get; set; }
}
public class TicketItem
{
public long Id { get; set; }
public long TicketId { get; set; }
public Ticket Ticket { get; set; }
}
}
```c#
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddEntityFrameworkInMemoryDatabase();
services.AddDbContext<AppDbContext>(
opts => { opts.UseInMemoryDatabase("app-db-context"); });
}
}
## Throw KeyNotFoundException
```c#
this._appDbContext.Set<Ticket>().Add(new Ticket { Items = new[] { new TicketItem() } });
await this._appDbContext.SaveChangesAsync();
// OK
var result = await this._appDbContext.Set<Ticket>().Include(t => t.Items).ToArrayAsync();
// Throw KeyNotFoundException
result = await this._appDbContext.Set<Ticket>().Include(_ => _.Items).ToArrayAsync();
EF Core version: 2.1.0-preview1-final
Database Provider: Microsoft.EntityFrameworkCore.InMemory
Operating system: Win 10
IDE: Visual Studio 2017 15.6.5
Same happens with correlated subquery too.
querySource item name to be __ which is reserved prefix for parameters in QCC.
Putting on the backlog. Workaround is to not use "_".
That's a weird bug... I wouldn't have expected the name of the lambda parameter to have any effect. Will this be fixed for 3.0?
@thomaslevesque This issue is in the Backlog milestone. This means that it is not going to happen for the 3.0 release. We will re-assess the backlog following the 3.0 release and consider this item at that time. However, keep in mind that there are many other high priority features with which it will be competing for resources.
@ajcvickers sure, I understand. It's not a very big issue anyway, and there's an easy workaround.
This is working in 3.1
Most helpful comment
Putting on the backlog. Workaround is to not use "_".