ADO.NET 实体框架和 WCF 服务
我有一个简单的 ADO.NET Entity Framework 4.0 模型 (edmx),它定义具有外键关系的数据库表。
如何将这些实体发送到 Windows Phone 7 客户端?我创建了一个 WCF 服务(使用 WShttpbinding),使用以下方法...
public List<LocationCity> ListCities()
{
var dc = ObjectFactory.GetInstance<TestEntities>();
var locs = dc.LocationCities.Take(10).ToList();
return locs;
}
我还创建了一个简单的控制台应用程序来使用该服务,但它不起作用...在跟踪中我看到了异常
可容纳的最大项目数 序列化或反序列化 对象图是“65536”
在 System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeParameterPart
然后我将 MaxItemsInObjectGraph 更改为一个很大的数字,只是为了看看会发生什么,然后我得到了堆栈溢出异常。因此,在我看来,dataContractSerializer 正在导航对象图上的循环属性并进入递归循环。
我想要做的就是将 10 个 LocationCity 实体发送到客户端(无论是 Windows Phone 还是控制台)。
我想我可以创建单独的 DataContract POCO 实体,并从上下文中的选择中填充它们...但是,我真的不想无缘无故地重复类。我想我一定做错了什么。
我真的很感激一些帮助!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
处理循环引用的技巧是使用[DataContract(IsReference=true)]。 IsReference 属性自 .NET 3.5 SP1 起可用,因此它对您来说应该不是问题。
最好的问候,拉迪斯拉夫
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
我已在实体设计器中将“LazyLoadingEnabled”设置为 true。只需将其设置为 false,一切都很好!
I had set "LazyLoadingEnabled" to true within entities designer. Just had to set it to false and everything is fine!