WCF保存EF 4.1项目抛出异常无法将项目添加到固定大小的类型数组中

San*_*anj 3 wcf savechanges entity-framework-4 dbcontext

我已经搜索了谷歌并且花了很多时间试图弄清楚我的WCF和客户端Windows窗体应用程序发生了什么.

我不断收到以下错误

"Unable to set field/property Ingredients on entity type Datalayer.UnitOfMeasure. See InnerException for details."
...
inner exception is 
"An item cannot be added to a fixed size Array of type 'Datalayer.Ingredient[]'."


Stack Trace - 
    at System.Data.Objects.Internal.PocoPropertyAccessorStrategy.<AddToCollection>b__0[T](Object collectionArg, Object item)
       at System.Data.Objects.Internal.PocoPropertyAccessorStrategy.CollectionAdd(RelatedEnd relatedEnd, Object value)
Run Code Online (Sandbox Code Playgroud)

我配置我的解决方案的方式我有一个引用我的DataLayer类库的WCF Web服务,我有一个Windows应用程序(测试应用程序),它引用了WCF服务以及DataLayer项目.

如果我没有在我的测试应用程序中引用DataLayer,则不会发生此问题,但是我会丢失ICollection<Ingredient>到简单Ingredient[]数组.现在你可以看到,每次初始化数组都会成为编码的痛苦.

任何想法的人?提前致谢.

Ken*_*ith 8

我遇到了这个确切的错误,但接受的答案并不是我需要的解决方案.事实证明,在客户端被发送List<Order>到我的WCF服务,但因为Customer.Orders财产被定义为ICollection<Order>,在WCF解串器解串器只是反序列化它在它可以最简单的形式,这是Order类型,即数组,Order[].

我能想到的唯一解决方案是将我的POCO对象上的属性从a ICollection<Order>更改为a HashSet<Order>.有关更多详细信息,请参阅停止WCF将空ICollection反序列化为零容量阵列.