You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// See https://aka.ms/new-console-template for more informationusingMicrosoft.Data.Sqlite;usingMicrosoft.EntityFrameworkCore;varconnection=newSqliteConnection("DataSource=:memory:");connection.Open();varcontext=newApplicationDbContext(newDbContextOptionsBuilder<ApplicationDbContext>().UseSqlite(connection).Options);context.Database.EnsureCreated();varmyDataWithList=newMyModelWithList{MyEnums=newList<MyEnum>{MyEnum.Test1,MyEnum.Test2}};context.MyModelWithLists.Add(myDataWithList);awaitcontext.SaveChangesAsync();context.ChangeTracker.Clear();vardataWithList=awaitcontext.MyModelWithLists.FirstAsync();// no error// ERORR BELOWvarmyData=newMyModel{MyEnums=newList<MyEnum>{MyEnum.Test1,MyEnum.Test2}};context.MyModels.Add(myData);awaitcontext.SaveChangesAsync();context.ChangeTracker.Clear();vardata=awaitcontext.MyModels.FirstAsync();// error here// CLASS DEFINITIONSpublicenumMyEnum{Test1,Test2,Test3}publicclassMyModel{publicGuidId{get;set;}publicICollection<MyEnum>MyEnums{get;set;}=[];}publicclassMyModelWithList{publicGuidId{get;set;}publicList<MyEnum>MyEnums{get;set;}=[];}publicsealedclassApplicationDbContext:DbContext{publicApplicationDbContext(DbContextOptions<ApplicationDbContext>options):base(options){}publicDbSet<MyModel>MyModels{get;set;}=null!;publicDbSet<MyModelWithList>MyModelWithLists{get;set;}=null!;protectedoverridevoidOnModelCreating(ModelBuildermodelBuilder){modelBuilder.Entity<MyModel>().HasKey(x =>x.Id);modelBuilder.Entity<MyModel>().Property(x =>x.MyEnums).HasMaxLength(1024);modelBuilder.Entity<MyModelWithList>().HasKey(x =>x.Id);modelBuilder.Entity<MyModelWithList>().Property(x =>x.MyEnums).HasMaxLength(1024);}}
Stack traces
Unhandled exception. System.ArgumentException: Expression of type 'System.Collections.Generic.ICollection`1[EFCoreICollectionBug.MyEnum]' cannot be used for parameter of type 'System.Collections.Generic.IList`1[EFCoreICollectionBug.MyEnum]' of method 'System.Collections.Generic.IList`1[EFCoreICollectionBug.MyEnum] PopulateList[MyEnum](System.Collections.Generic.IList`1[EFCoreICollectionBug.MyEnum], System.Collections.Generic.IList`1[EFCoreICollectionBug.MyEnum])' (Parameter 'arg0')
at System.Dynamic.Utils.ExpressionUtils.ValidateOneArgument(MethodBase method, ExpressionType nodeKind, Expression arguments, ParameterInfo pi, String methodParamName, String argumentParamName, Int32 index)
at System.Linq.Expressions.Expression.Call(MethodInfo method, Expression arg0, Expression arg1)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityMaterializerSource.<AddInitializeExpressions>g__CreateMemberAssignment|14_0(Expression parameter, MemberInfo memberInfo, IPropertyBase property, Expression value)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityMaterializerSource.AddInitializeExpressions(HashSet`1 properties, ParameterBindingInfo bindingInfo, Expression instanceVariable, List`1 blockExpressions)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityMaterializerSource.CreateMaterializeExpression(List`1 blockExpressions, ParameterExpression instanceVariable, Expression constructorExpression, HashSet`1 properties, ParameterBindingInfo bindingInfo)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityMaterializerSource.CreateMaterializeExpression(EntityMaterializerSourceParameters parameters, Expression materializationContextExpression)
at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.EntityMaterializerInjectingExpressionVisitor.CreateFullMaterializeExpression(ITypeBase concreteTypeBase, ValueTuple`4 materializeExpressionContext)
at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.EntityMaterializerInjectingExpressionVisitor.MaterializeEntity(StructuralTypeShaperExpression shaper, ParameterExpression materializationContextVariable, ParameterExpression concreteEntityTypeVariable, ParameterExpression instanceVariable, ParameterExpression entryVariable)
at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.EntityMaterializerInjectingExpressionVisitor.ProcessEntityShaper(StructuralTypeShaperExpression shaper)
at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.EntityMaterializerInjectingExpressionVisitor.VisitExtension(Expression extensionExpression)
at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.EntityMaterializerInjectingExpressionVisitor.Inject(Expression expression)
at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.InjectEntityMaterializers(Expression expression)
at Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.VisitExtension(Expression extensionExpression)
at Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.ProcessShaper(Expression shaperExpression, RelationalCommandCache& relationalCommandCache, IReadOnlyList`1& readerColumns, LambdaExpression& relatedDataLoaders, Int32& collectionId)
at Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.VisitShapedQuery(ShapedQueryExpression shapedQueryExpression)
at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.VisitExtension(Expression extensionExpression)
at Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.VisitExtension(Expression extensionExpression)
at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass12_0`1.<ExecuteAsync>b__0()
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync[TResult](Expression query, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.ExecuteAsync[TResult](Expression expression, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ExecuteAsync[TSource,TResult](MethodInfo operatorMethodInfo, IQueryable`1 source, Expression expression, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ExecuteAsync[TSource,TResult](MethodInfo operatorMethodInfo, IQueryable`1 source, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.FirstAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)
at Program.<Main>$(String[] args) in D:\Projekty\EFCoreICollectionBug\EFCoreICollectionBug\Program.cs:line 38
at Program.<Main>(String[] args)
Verbose output
EF Core version
8.0.6
Database provider
Microsoft.EntityFrameworkCore.Sqlite
Target framework
.Net 8
Operating system
Windows 11
IDE
Rider 2024
The text was updated successfully, but these errors were encountered:
You have a zip in Issue which can help you with reproducing the Issue. Also somewhere in EF Core there is PopulateList method which is causing a problem.
Yeah I saw it, but I think the issue might be in VisitBinary in RelationalShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.cs that is calling PopulateList with an ICollection
SamMonoRT
changed the title
Primitive Collections doesn't work with ICollection, while querying data.
[EF Core 8.x] - Primitive Collections doesn't work with ICollection, while querying data.
Jan 21, 2025
Can I work on this ? if you could allow me and guide me through it please
Kind regards
We welcome all community contributions. Please make a fix on EFcore main. Once you have a locally validated fix and PR ready for review, let us know and we can review and determine possibility of patching 8.0.x builds.
Bug description
Primitive Collections doesn't work with ICollection, while querying data.
According to this example it should work.
Note: IList, List and T[] works.
EFCoreICollectionBug.zip
Your code
Stack traces
Verbose output
EF Core version
8.0.6
Database provider
Microsoft.EntityFrameworkCore.Sqlite
Target framework
.Net 8
Operating system
Windows 11
IDE
Rider 2024
The text was updated successfully, but these errors were encountered: