diff --git a/src/Blueshift.EntityFrameworkCore.MongoDB/Storage/MongoDbDatabase.cs b/src/Blueshift.EntityFrameworkCore.MongoDB/Storage/MongoDbDatabase.cs index 0187865..7456b4d 100644 --- a/src/Blueshift.EntityFrameworkCore.MongoDB/Storage/MongoDbDatabase.cs +++ b/src/Blueshift.EntityFrameworkCore.MongoDB/Storage/MongoDbDatabase.cs @@ -141,7 +141,10 @@ private async Task UpdateEntriesAsync( /// public override Func> CompileAsyncQuery(QueryModel queryModel) - => queryContext => CompileQuery(queryModel)(queryContext).ToAsyncEnumerable(); + { + var syncQueryExecutor = CompileQuery(queryModel); + return queryContext => syncQueryExecutor(queryContext).ToAsyncEnumerable(); + } private IUpdateEntry GetRootDocument(InternalEntityEntry entry) { diff --git a/test/Blueshift.EntityFrameworkCore.MongoDB.Tests/MongoDbContextTests.cs b/test/Blueshift.EntityFrameworkCore.MongoDB.Tests/MongoDbContextTests.cs index 66a9280..a15041b 100644 --- a/test/Blueshift.EntityFrameworkCore.MongoDB.Tests/MongoDbContextTests.cs +++ b/test/Blueshift.EntityFrameworkCore.MongoDB.Tests/MongoDbContextTests.cs @@ -283,6 +283,38 @@ await zooDbContext.Animals }); } + [Fact] + public async Task Can_execute_same_async_query_from_different_dbContext_instances() + { + await ExecuteUnitOfWorkAsync(async zooDbContext => + { + zooDbContext.Animals.AddRange(_zooEntities.Animals); + Assert.Equal( + _zooEntities.Entities.Count, + await zooDbContext.SaveChangesAsync(acceptAllChangesOnSuccess: true)); + }); + + Func>> query = (zooDbContext) => + zooDbContext.Animals + .OrderBy(animal => animal.Name) + .ThenBy(animal => animal.Height) + .ToListAsync(); + + await ExecuteUnitOfWorkAsync(async zooDbContext => + { + Assert.Equal(_zooEntities.Animals, + await query.Invoke(zooDbContext), + new AnimalEqualityComparer()); + }); + + await ExecuteUnitOfWorkAsync(async zooDbContext => + { + Assert.Equal(_zooEntities.Animals, + await query.Invoke(zooDbContext), + new AnimalEqualityComparer()); + }); + } + [Fact] public async Task Can_query_first_or_default_async() {