Efcore: `Collection was of a fixed size.` in Sqlite InMemory provider

Created on 11 Sep 2017  路  5Comments  路  Source: dotnet/efcore

I have query like below:

```c#
var exist = await _context.ProductGroups.AnyAsync(pg => pg.ProductGroupTypeId == groupTypeId && pg.Title == title);


and query throw exception:

Exception message:
System.NotSupportedException: Collection was of a fixed size.
at System.ThrowHelper.ThrowNotSupportedException(ExceptionResource resource)
at Microsoft.EntityFrameworkCore.Metadata.Internal.ClrICollectionAccessor3.Add(Object instance, Object value) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NavigationFixer.AddToCollection(InternalEntityEntry entry, INavigation navigation, IClrCollectionAccessor collectionAccessor, Object value) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NavigationFixer.ToDependentFixup(InternalEntityEntry dependentEntry, InternalEntityEntry principalEntry, IForeignKey foreignKey) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NavigationFixer.InitialFixup(InternalEntityEntry entry, ISet1 handledForeignKeys, Boolean fromQuery)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NavigationFixer.StateChanged(InternalEntityEntry entry, EntityState oldState, Boolean fromQuery)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntryNotifier.StateChanged(InternalEntityEntry entry, EntityState oldState, Boolean fromQuery)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityGraphAttacher.PaintAction(EntityEntryGraphNode node)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityEntryGraphIterator.TraverseGraph(EntityEntryGraphNode node, Func2 handleNode) at Microsoft.EntityFrameworkCore.DbContext.SetEntityState[TEntity](TEntity entity, EntityState entityState) at ProductGroupRepository.AddProductGroup(ProductGroupEntity productGroup) in \ProductGroupRepository.cs:line 20 at SociaSoft.Bss.Infrastructure.Products.ProductGroupService.<AddProductGroupAsync>d__3.MoveNext() in \ProductGroupService.cs:line 26 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult()
at Products.ProductGroupCommandHandler.d__3.MoveNext() in \ProductGroupCommandHandler.cs:line 24
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Controllers.ProductGroupsController.d__5.MoveNext() in
Controllers\ProductGroupsController.cs:line 50
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__12.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__10.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__14.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.d__22.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.d__17.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.d__15.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Builder.RouterMiddleware.d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
```

EF Core version: 2.0.0
Database Provider: Microsoft.Data.Sqlite 2.0.0
Operating system: Windows 10
IDE: Visual Studio 2017

closed-question

Most helpful comment

@mo-esmp In DataContextSeed the code is creating a new array of ProductGroupEntity:
```C#
ProductGroups = new[]
{
new ProductGroupEntity
...

EF navigation properties cannot be fixed sized arrays. so this needs to be changed to something like:
```C#
ProductGroups = new List<ProductGroupEntity>
{
    new ProductGroupEntity
    ...

With this change the test passes for me.

All 5 comments

@mo-esmp What do your entity types look like? Do they have fixed-size collections for navigation properties?

Here is my entity:
```c#
public class ProductGroupEntity : BaseEntity
{
public string Title { get; set; }

public bool IsActive { get; set; }

public byte[] Icon { get; set; }

public int? ParentId { get; set; }
public ProductGroupEntity Parent { get; set; }

public int ProductGroupTypeId { get; set; }
public ProductGroupTypeEntity ProductGroupType { get; set; }

public ICollection<ProductGroupProvinceEntity> ProductGroupProvinces { get; set; }

public ICollection<ProductEntity> Products { get; set; }

}
```

Query works fine with SQL Server provider and for testing purpose I use Sqlite in memory provider. I think this error is related to Icon property.

@mo-esmp Thanks for the additional info. Unfortunately we still are not able to reproduce this issue. Could you provide a full code listing or project that demonstrates the issue?

@ajcvickers I couldn't reproduce issue in a sample but still occurring in my solution and I didn't find any reason for that or what I'm doing wrong. here is my simplified solution repo url: SqliteDemo.

just run test in AdminProductGroupTests.cs file and error will occur in
https://github.com/mo-esmp/SqliteDemo/blob/ebe0adde69c52e72ea38418b8df75ec719812218/src/Infrastructure/Products/ProductGroup/ProductGroupRepository.cs#L25

@mo-esmp In DataContextSeed the code is creating a new array of ProductGroupEntity:
```C#
ProductGroups = new[]
{
new ProductGroupEntity
...

EF navigation properties cannot be fixed sized arrays. so this needs to be changed to something like:
```C#
ProductGroups = new List<ProductGroupEntity>
{
    new ProductGroupEntity
    ...

With this change the test passes for me.

Was this page helpful?
0 / 5 - 0 ratings