简体   繁体   English

ADO.NET实体框架和WCF服务

[英]ADO.NET Entity Framework and WCF Service

I have a simple ADO.NET Entity Framework 4.0 model (edmx) which defines database tables with foreign key relationships. 我有一个简单的ADO.NET实体框架4.0模型(edmx),该模型定义具有外键关系的数据库表。

How can I send these entities down to a Windows Phone 7 client? 如何将这些实体向下发送到Windows Phone 7客户端? I have created a WCF Service (using WShttpbinding),with the method... 我已经使用以下方法创建了WCF服务(使用WShttpbinding)...

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' 在对象图中可以序列化或反序列化的最大项目数为'65536'

at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeParameterPart 在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. 然后,我将MaxItemsInObjectGraph更改为大量,只是想知道会发生什么,然后我得到了堆栈溢出异常。 So it looks to me that the dataContractSerializer is navigating cyclic properties on the object graph and getting into a recursive loop. 因此在我看来,dataContractSerializer正在导航对象图上的循环属性并进入递归循环。

All I want to do is send 10 LocationCity entities down to the client (whether Windows Phone or Console). 我要做的就是将10个LocationCity实体发送到客户端(Windows Phone或控制台)。

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. 我想我可以创建单独的DataContract POCO实体,并从上下文中的select中填充它们……但是,我真的不想无缘无故地重复类。 I figure I must be doing something wrong. 我想我一定做错了。

I would really appreciate some help! 我真的很感谢您的帮助!

The trick to deal with circular references is to use [DataContract(IsReference=true)]. 处理循环引用的技巧是使用[DataContract(IsReference = true)]。 IsReference property is available since .NET 3.5 SP1 so it should not be problem for you. 从.NET 3.5 SP1开始,IsReference属性可用,因此对您来说应该不成问题。

Best regards, Ladislav 最好的问候,拉迪斯拉夫

I had set "LazyLoadingEnabled" to true within entities designer. 我在实体设计器中将“ LazyLoadingEnabled”设置为true。 Just had to set it to false and everything is fine! 只需将其设置为false,一切都很好!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM