尝试通过WCF提交EF4实体时获取序列化错误 [英] Get serialization error when try and submit EF4 entity via WCF

查看:250
本文介绍了尝试通过WCF提交EF4实体时获取序列化错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体CustomerActivityReport,我试图通过WCF提交到服务器。在服务器端,我使用repository + UOW模式将实体更新/插入到数据库中。

I have an entity CustomerActivityReport which I'm trying to submit to the server via WCF. On the server end I'm using the repository + UOW patterns to update/insert the entity into the db.

CustomerActivityReport与另一个实体LookupValue有多对多关系。当我尝试并提交一个CustomerActivityReport的实例时,DataContractSerializer会抛出错误:对象类型为FixupCollection [CustomerActivityReport]'包含循环,如果禁用了引用跟踪,则不能被序列化。我得到这个错误,即使我不设置关系LookupValue实体。

CustomerActivityReport has a many to many relationship to another entity LookupValue. When I try and submit an instance of CustomerActivityReport, the DataContractSerializer throws the error: "Object graph for type 'FixupCollection[CustomerActivityReport]' contains cycles and cannot be serialized if reference tracking is disabled". I am getting this error even when I don't set the relationship on the LookupValue entities.

为了解决这个问题,我试过应用[DataContract(IsReference = true) ]到所讨论的实体和FixupCollection。但是我遇到了不同的问题。

To get around this I've tried applying [DataContract(IsReference = true)] to both the entities in question and also to FixupCollection. But then I get different problems.

有人在尝试通过WCF提交相关实体时遇到类似的问题吗?

Has anybody else run into similar problems when trying to submit related entities over WCF?

先感谢任何回复。

Ryan

推荐答案

我无法得到这个工作与FixupCollection,所以我不得不提交所有的实体集合作为标准的集合,然后添加逻辑服务器端,将它们改回FixupCollection。

I couldn't get this working with FixupCollection, and so I've had to submit all my entity collections as standard Collection, and then add logic server end to change them back into FixupCollection.

客户:

convertedCustomerActivityReport.LookupValues = new Collection<LookupValue>()

服务器:

public virtual ICollection<LookupValue> LookupValues
    {
        get
        {
            if (_lookupValues == null || _lookupValues is Array)
            {
                var newCollection = new FixupCollection<LookupValue>();
                newCollection.CollectionChanged += FixupLookupValues;
                newCollection.AddRange(_lookupValues);
                _lookupValues = newCollection;
            }
            return _lookupValues;
        }



我还为FixupCollection添加了一个AddRange方法:

I've also added an AddRange method to FixupCollection:

 /// <summary>
    /// Adds multiple items.
    /// </summary>
    /// <param name="items">The items to add.</param>
    public void AddRange(IEnumerable<T> items)
    {
        if (items == null)
        {
            return;
        }

        foreach (var item in items)
        {
            this.Add(item);
        }
    }

这篇关于尝试通过WCF提交EF4实体时获取序列化错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆