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.

ApplyQueryHints throws Collection was modified; enumeration operation may not execute

See original GitHub issue

I use _context.NextQueryHints.Add(SqlServerTools.Sql.OptionRecompile); in several part of my code. and I register the connection as scoped in DI. sometimes when my services are under load, I get this exception

Exception message:
Collection was modified; enumeration operation may not execute.
Stack trace:
   `"trace":"   at System.Collections.Generic.List`1.Enumerator.MoveNextRare()\r\n   at LinqToDB.SqlProvider.BasicSqlBuilder.ApplyQueryHints(String sql, List`1 queryHints)\r\n   at LinqToDB.Data.DataConnection.InitCommand(CommandType commandType, String sql, DataParameter[] parameters, List`1 queryHints, Boolean withParameters)\r\n   at LinqToDB.Data.DataConnection.QueryRunner.ExecuteReaderAsync(CancellationToken cancellationToken)\r\n   at LinqToDB.Linq.QueryRunner.ExecuteQueryAsync[T](Query query, IDataContext dataContext, Mapper`1 mapper, Expression expression, Object[] ps, Object[] preambles, Int32 queryNumber, Func`2 func, TakeSkipDelegate skipAction, TakeSkipDelegate takeAction, CancellationToken cancellationToken)\r\n   at LinqToDB.Linq.ExpressionQuery`1.GetForEachAsync(Action`1 action, CancellationToken cancellationToken)\r\n   at LinqToDB.AsyncExtensions.ToListAsync[TSource](IQueryable`1 source, CancellationToken token)\r\n   at WebApi.Commands.MarketInfo.StockVolumeChartReadingCommand.StockVolumeChartReadingHandler.Handle(StockVolumeChartReadingCommand request, CancellationToken cancellationToken) in D:\\Workspaces\\New Mdp\\WebApi\\Commands\\MarketInfo\\StockVolumeChartReadingCommand.cs:line 69\r\n   at MediatR.Pipeline.RequestExceptionProcessorBehavior`2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate`1 next)\r\n   at MediatR.Pipeline.RequestExceptionProcessorBehavior`2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate`1 next)\r\n   at MediatR.Pipeline.RequestExceptionActionProcessorBehavior`2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate`1 next)\r\n   at MediatR.Pipeline.RequestExceptionActionProcessorBehavior`2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate`1 next)\r\n   at MediatR.Pipeline.RequestPostProcessorBehavior`2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate`1 next)\r\n   at MediatR.Pipeline.RequestPreProcessorBehavior`2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate`1 next)\r\n   at WebApi.Controllers.MarketInfoController.StockVolumeChartReadingCommand(StockVolumeReadingChartRequest request) in D:\\Workspaces\\New Mdp\\WebApi\\Controllers\\MarketInfoController.cs:line 31\r\n   at lambda_method1763(Closure , Object )\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()\r\n--- End of stack trace from previous location ---\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)\r\n   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)\r\n   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)\r\n   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)\r\n   at WebApi.Middlewares.ApiExceptionHandlingMiddleware.Invoke(HttpContext context) in D:\\Workspaces\\New Mdp\\WebApi\\Middlewares\\ApiExceptionHandlingMiddleware.cs:line 37",`

there is one part of my code that I use OptionRecompile :

 var query = _context.FinalScreeners.With("NoLock").AsQueryable();
                query = query.Where(x => x.TradeDate >= request.Request.FromDate);
                query = query.Where(x => x.TradeDate.Date <= request.Request.ToDate);
                _context.NextQueryHints.Add(SqlServerTools.Sql.OptionRecompile);

                return await query.Select(x => new StockVolumeReadingChartResponse
                {
                    TradeDate = x.TradeDate,
                    Isin = x.Isin,
                }).SetOrdering(request.Request.Sort).ToListAsync(token: cancellationToken);

Environment details

linq2db version: 3.2.3 Database Server: Sql Server 2019 Database Provider: ? Operating system: Windows Server 2019 .NET Framework: Net 5.0.2

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
sdanylivcommented, Jan 20, 2021

Never change MappingSchema.Default in that way, Create new MappingSchema and add to DataConnection

var ms = new MappingSchema();
var mapper = ms.GetFluentMappingBuilder();
RealLegalTradeHistoricalFluentMap.Map(mapper);
...
services.AddLinqToDbContext<AppDataConnection>((provider, options) =>
           {
               options
               .UseSqlServer(currentConnectionString)
               .UseMappingSchema(ms);

               if (_logDbCommands)
                   options.UseDefaultLogging(provider);
           });
0reactions
MostafaEsmaeilicommented, Jan 20, 2021

Update: I have another exception: Collection was modified; enumeration operation may not execute this time it accrues on fluent map class :

this is fluentMap class :

public static class RealLegalTradeHistoricalFluentMap
    {
        public static void Map(FluentMappingBuilder mapper)
        {
            mapper.Entity<RealLegalTradeHistoricalEntity>().HasTableName("RealLegalTradeHistorical")
                .HasSchemaName("dbo")
                .Property(x => x.Id).IsPrimaryKey().IsIdentity().HasSkipOnInsert().HasSkipOnUpdate()
           .Property(x => x.Isin)
                .HasLength(12)
                .HasDataType(LinqToDB.DataType.VarChar)
                .IsNullable(false)

            .Property(x => x.InsCode)
                 .HasDataType(LinqToDB.DataType.VarChar)
                 .HasLength(50)
                 .IsNullable(false)

            .Property(x => x.RealSellVolume)
                .IsNullable()
                .HasPrecision(18).HasScale(0)

            .Property(x => x.RealSellACount)
                .IsNullable()
                .HasPrecision(18).HasScale(0)

            .Property(x => x.RealBuyVolume)
                .IsNullable()
                .HasPrecision(18).HasScale(0)

            .Property(x => x.RealBuyCount)
                .IsNullable()
                .HasPrecision(18).HasScale(0)

            .Property(x => x.LegalSellVolume)
                .IsNullable()
                .HasPrecision(18).HasScale(0)

            .Property(x => x.LegalSellACount)
                .IsNullable()
                .HasPrecision(18).HasScale(0)

            .Property(x => x.LegalBuyVolume)
                .IsNullable()
                .HasPrecision(18).HasScale(0)

            .Property(x => x.LegalBuyCount)
                .IsNullable()
                .HasPrecision(18).HasScale(0)

            .Property(x => x.Date)
                .IsNullable(false)

            .Property(x => x.Created)
                .IsNullable();
        }

    }

I Add this class to my context this way :

 public AppDataConnection(LinqToDbConnectionOptions<AppDataConnection> options)
            : base(options)
        {
            var mapper = MappingSchema.Default.GetFluentMappingBuilder();
            ...
            RealLegalTradeHistoricalFluentMap.Map(mapper);
            ...
          
        }

and new StackTrace :


 ---> System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
   at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
   at System.Linq.Enumerable.OfTypeIterator[TResult](IEnumerable source)+MoveNext()
   at System.Collections.Generic.LargeArrayBuilder`1.AddRange(IEnumerable`1 items)
   at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source)
   at LinqToDB.Metadata.FluentMetadataReader.GetAttributes[T](Type type, MemberInfo memberInfo, Boolean inherit)
   at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.ToArray()
   at System.Linq.Enumerable.SelectManyIterator[TSource,TCollection,TResult](IEnumerable`1 source, Func`2 collectionSelector, Func`3 resultSelector)+MoveNext()
   at System.Collections.Generic.LargeArrayBuilder`1.AddRange(IEnumerable`1 items)
   at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source)
   at LinqToDB.Mapping.MappingSchema.GetAttributes[T](Type type, MemberInfo memberInfo, Func`2 configGetter, Boolean inherit, Boolean exactForConfiguration)
   at LinqToDB.Mapping.EntityMappingBuilder`1.<SetAttribute>g__SetAttr|48_0[TProperty,TA](Expression e, Boolean m, <>c__DisplayClass48_0`2& )
   at LinqToDB.Mapping.EntityMappingBuilder`1.SetAttribute[TProperty,TA](Expression`1 func, Boolean processNewExpression, Func`2 getNew, Action`2 modifyExisting, Func`2 configGetter, Func`2 overrideAttribute, Func`2 existingGetter)
   at LinqToDB.Mapping.PropertyMappingBuilder`2.SetColumn(Action`1 setColumn)
   at CommonObjects.Linq2DbSettings.Mapping.RealLegalTradeHistoricalFluentMap.Map(FluentMappingBuilder mapper) in D:\Project\CommonObjects\Linq2DbSettings\Mapping\RealLegalTradeHistoricalFluentMap.cs:line 67
   at CommonObjects.Linq2DbSettings.AppDataConnection..ctor(LinqToDbConnectionOptions`1 options) in D:\Project\CommonObjects\Linq2DbSettings\AppDataConnection.cs:line 80

I emphasize again that this service works properly in most cases with this configuration. and now when I change my fluent map classes and remove all property configuration, The services work properly under load (till now)

 public static void Map(FluentMappingBuilder mapper)
        {
            mapper.Entity<RealLegalTradeHistoricalEntity>().HasTableName("RealLegalTradeHistorical")
                .HasSchemaName("dbo")
                .Property(x => x.Id).IsPrimaryKey().IsIdentity().HasSkipOnInsert().HasSkipOnUpdate();
         
        }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Collection was modified; enumeration operation may not ...
So the issue behind this error is, we can not modify the list/dictionary while we are looping through the same. One of the...
Read more >
Why the error Collection was modified enumeration ...
This error occurs when a looping process is being running on a collection (Ex: List) and the collection is modified (data added or...
Read more >
Collection Was Modified Enumeration Operation May Not ...
The collection was modified enumeration operation may not execute exception occurs when you modify the collection while looping through the same.
Read more >
Collection was modified; enumeration operation may not ...
Solution 1 – If you're removing items, use RemoveAll() · Solution 2 – If you're adding items, put them in a temp and...
Read more >
enumeration operation may not execute. At foreach
Solution 2​​ You cannot enumerate a collection in a foreach block and modify the collection in the same block.
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