efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Error querying two DbSets with NoTracking QueryTrackingBehavior

Open Jtfs opened this issue 7 years ago • 4 comments

The code below throws an exception. It works in Entity Framework Core 2.0.3 and with default QueryTrackingBehavior configuration.

class Program
    {
        static void Main(string[] args)
        {
            using (var context = new AppDbContext())
            {
                if (context.Database.EnsureCreated())
                {
                    context.Add(new EntityA());
                    context.Add(new EntityB());

                    context.SaveChanges();
                }

                context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;

                foreach (var entityA in context.EntitiesA)
                {
                    EntityB entityB = context.EntitiesB.First(); //Throws a System.InvalidOperationException
                }
            }
        }
    }

    public class AppDbContext : DbContext
    {
        public DbSet<EntityA> EntitiesA { get; set; }
        public DbSet<EntityB> EntitiesB { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlite("Data Source=test.db");
        }
    }

    public class EntityA
    {
        public int Id { get; set; }
    }

    public class EntityB
    {
        public int Id { get; set; }
    }

Exception message:

System.InvalidOperationException: 'A second operation started on this context before a previous operation completed. 
This is usually caused by different threads using the same instance of DbContext, however instance members are not guaranteed to be thread safe. 
This could also be caused by a nested query being evaluated on the client, if this is the case rewrite the query avoiding nested invocations.'

Stack trace:

   at Microsoft.EntityFrameworkCore.Internal.ConcurrencyDetector.EnterCriticalSection()
   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
   at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1 source, Boolean& found)
   at System.Linq.Enumerable.First[TSource](IEnumerable`1 source)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass15_1`1.<CompileQueryCore>b__0(QueryContext qc)
   at System.Linq.Queryable.First[TSource](IQueryable`1 source)
   at TestEntityFrameworkCore.Program.Main(String[] args) in C:\Users\User\Projects\TestEntityFrameworkCore\TestEntityFrameworkCore\Program.cs:line 24

Steps to reproduce

Making a second query within a foreach

 foreach (var entityA in context.EntitiesA)
 {
     EntityB entityB = context.EntitiesB.First(); 
 }

Further technical details

EF Core version: 2.2.2 Database Provider: Microsoft.EntityFrameworkCore.Sqlite Operating system: Windows 10 1809 IDE: Visual Studio 2017 15.9.7

Jtfs avatar Feb 20 '19 14:02 Jtfs

Notes for triage:

  • Repros on SQLite (MARS always) and SQL Server (MARS or not)
  • Doesn't throw for tracking queries (MARS or not)

ajcvickers avatar Feb 25 '19 20:02 ajcvickers

I can reproduce this with SQLite and tracking on as well :(

adrianhara avatar May 08 '19 10:05 adrianhara

@adrianhara This issue is specifically about no-tracking queries. If you are seeing a similar problem with tracking queries, then please file a new issue and include a small, runnable project/solution or complete code listing that demonstrates the problem.

ajcvickers avatar May 08 '19 13:05 ajcvickers

The reported code already works on Entity Framework Core 3.0.0

Thanks!

Jtfs avatar Sep 26 '19 09:09 Jtfs