Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework 'An item cannot be removed from a fixed size Array of type *[]'

I am using EntityFramework to persist my entities.

The problem I have is that when sending an entity constructed in Silverlight over a WCF to be peristed I get the error 'An item cannot be removed from a fixed size Array of type 'CarterShop.Commerce.Entities.ManufacturedItemRequirement[]'.

I am inserting an entity (ManufacturedItem) which has an ICollection of ManufacturedItemRequirements. Entity Framework should allow me to just 'Add' this to the DbContext as a POCO object and persist this away, but for some reason it complains about the collection.

Has anyone hit this problem before? Basically I do not know what it is complaining about. The error comes from inside System.Data:

at System.Data.Objects.Internal.PocoPropertyAccessorStrategy.CollectionRemove(RelatedEnd relatedEnd, Object value)
   at System.Data.Objects.Internal.EntityWrapper`1.CollectionRemove(RelatedEnd relatedEnd, Object value)
   at System.Data.Objects.DataClasses.EntityCollection`1.RemoveFromObjectCache(IEntityWrapper wrappedEntity)
   at System.Data.Objects.ObjectStateManager.DegradePromotedRelationships()
   at System.Data.Objects.DataClasses.RelationshipManager.AddRelatedEntitiesToObjectStateManager(Boolean doAttach)
   at System.Data.Objects.ObjectContext.AddObject(String entitySetName, Object entity)
   at System.Data.Entity.Internal.Linq.InternalSet`1.<>c__DisplayClass5.<Add>b__4()
   at System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName)
   at System.Data.Entity.Internal.Linq.InternalSet`1.Add(Object entity)
   at System.Data.Entity.DbSet`1.Add(TEntity entity)
   at Commerce.Model.Repositories.RepositoryBase`1.Add(T entity) in C:\OclProjects 4.1\CarterShop\CarterShop.Commerce.Model\Repositories\RepositoryBase.cs:line 28
   at CarterShop.Commerce.Services.Implementation.StockItemService.CreateManufacturedItem(ManufacturedItem manufactedItem, Boolean createDefinitionAswell) in C:\OclProjects 4.1\CarterShop\CarterShop.Commerce.Services\Implementation\StockItemService.cs:line 137

I am basically doing:

ManufacturedItem item = new ManufacturedItem();
item.ManufacturedItemRequirements.Add(new ManufacturedItemRequirement() { Quantity = 1; DefinitionId = 5 });

// Send to WCF...

Context.ManufacturedItems.Add(item); // Error thrown here.
Context.SaveChanges();
like image 211
James Avatar asked Apr 05 '11 19:04

James


2 Answers

the cause of this is that the default EF classes created are treated as HashSet and the relationship is denoted as of type ICollection. If you open your generated class in the constructor, for the related object list, append a ToList() method like so: public ResultSet() { this.Results = new HashSet().ToList(); // added the ToList() ..... }

then somewhere below you will see where its defined as a public virtual property like so: public virtual ICollection Results { get; set; } Change from ICollection to List

...this solved the similar problem i was having... however you must take note that your code will be overwritten if you re-generate the class/model from database

like image 29
Joel_J Avatar answered Nov 04 '22 21:11

Joel_J


There are other situations where you will encounter this error, including when deleting entities that were obtained via use of the .Include extension methods.

The key is that if the relationship is defined as an ICollection<T> then Entity Framework will materialize the relationship by using an array. However, there are many valid types you can choose from for relationships, including the types ISet<T> and IList<T>. Using either of these to define your relationships will solve the problem.

like image 97
Kirk Woll Avatar answered Nov 04 '22 22:11

Kirk Woll