问题
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 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.
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.
Has anybody else run into similar problems when trying to submit related entities over WCF?
Thanks in advance for any replies.
Ryan
回答1:
The times that we have had a similar problem we were missing an attribute on a sub object.
回答2:
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.
Client:
convertedCustomerActivityReport.LookupValues = new Collection<LookupValue>()
Server:
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;
}
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);
}
}
来源:https://stackoverflow.com/questions/4279434/get-serialization-error-when-try-and-submit-ef4-entity-via-wcf