views:

120

answers:

2

I have a simple ADO.NET Entity Framework 4.0 model (edmx) which defines database tables with foreign key relationships.

How can I send these entities down to a Windows Phone 7 client? I have created a WCF Service (using WShttpbinding),with the method...

public List<LocationCity> ListCities()
{    
   var dc = ObjectFactory.GetInstance<TestEntities>();
   var locs = dc.LocationCities.Take(10).ToList();
   return locs;
}

I also created a simple Console application to consume this service, but it doesn't work... In the trace I see the exception

Maximum number of items that can be serialized or deserialized in an object graph is '65536'

at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeParameterPart

I then changed MaxItemsInObjectGraph to a massive number, just to see what would happen and I get a stack overflow exception then. So it looks to me that the dataContractSerializer is navigating cyclic properties on the object graph and getting into a recursive loop.

All I want to do is send 10 LocationCity entities down to the client (whether Windows Phone or Console).

I suppose I could create separate DataContract POCO entities, and populate them from the select on the context... however, I don't really want to have to duplicate classes for no good reason. I figure I must be doing something wrong.

I would really appreciate some help!

+2  A: 

The trick to deal with circular references is to use [DataContract(IsReference=true)]. IsReference property is available since .NET 3.5 SP1 so it should not be problem for you.

Best regards, Ladislav

Ladislav Mrnka
This is already set by the entity designers. thanks anyway.
Boomerangertanger
A: 

I had set "LazyLoadingEnabled" to true within entities designer. Just had to set it to false and everything is fine!

Boomerangertanger