'An item cannot be deleted from a fixed size Array of type *[]', says Entity Framework.

.net entity-framework silverlight wcf

Question

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();
1
5
4/5/2011 8:40:46 PM

Accepted Answer

I was inserting the following graph:

ManufacturedItem has many ManufacturedItemRequirements.
ManufacturedItemRequirement has a ManufacturedItem.
ManufacturedItemRequirement has a StockItemDefinition.

This was being satisfied. However I was over looking one part of the graph which is:

StockItemDefinition has many ManufacturedItemRequirements.

Because I was not populating this reference array with the new ManufacturedItemRequirement, it would not work.

I do think I should not have to specify this, as that relationship is an implicit rather than explicit relationship. I think rather than generating an empty array for these properties it should generate NULL. If I set the child array to NULL the Add operation works.

I will let you know if I manage to change the way it is serialized.

0
4/6/2011 9:11:32 AM

Popular Answer

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.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow