【问题标题】:Get serialization error when try and submit EF4 entity via WCF尝试通过 WCF 提交 EF4 实体时出现序列化错误
【发布时间】:2010-11-25 17:10:19
【问题描述】:

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

CustomerActivityReport 与另一个实体 LookupValue 具有多对多关系。当我尝试提交 CustomerActivityReport 的实例时,DataContractSerializer 抛出错误:“'FixupCollection[CustomerActivityReport]' 类型的对象图包含循环,如果禁用引用跟踪,则无法序列化”。即使我没有在 LookupValue 实体上设置关系,我也会收到此错误。

为了解决这个问题,我尝试将 [DataContract(IsReference = true)] 应用于相关实体和 FixupCollection。但后来我遇到了不同的问题。

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

提前感谢您的任何回复。

瑞恩

【问题讨论】:

    标签: wcf c#-4.0 entity-framework-4


    【解决方案1】:

    我们遇到过类似问题的时候,我们缺少子对象的属性。

    【讨论】:

      【解决方案2】:

      我无法使用 FixupCollection 进行此操作,因此我必须将我的所有实体集合作为标准集合提交,然后添加逻辑服务器端以将它们改回 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 方法:

       /// <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);
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-12
        • 2012-10-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多