在序列化Entity Framework 4 POCO类时使用Include()时出现问题 [英] Problem using Include() when serializing Entity Framework 4 POCO classes

查看:93
本文介绍了在序列化Entity Framework 4 POCO类时使用Include()时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有Entity Framework 4模型的WCF服务,使用序列化并发送到客户端应用程序的POCO类。我将LazyLoadingEnabled和ProxyCreationEnabled设置为false,我使用Linq to Entites查询实体,并通过List<>返回
给客户。当我不使用Include()时,一切都很完美:

I have a WCF service with an Entity Framework 4 model, using POCO classes that are serialized and sent over to client applications. I have LazyLoadingEnabled and ProxyCreationEnabled set to false, and I'm using Linq to Entites to query an Entity, and return it via List<> to the client. Everything goes perfect when I don't use Include():

 


public List<TBLTable1> GetTBLTable1(string pCode)
{
 using (PcFactoryEntities oPcFactoryDB = new PcFactoryEntities())
 {
  oPcFactoryDB.ContextOptions.ProxyCreationEnabled = false;
  oPcFactoryDB.ContextOptions.LazyLoadingEnabled = false;
  var oRS = oPcFactoryDB.TBLTable1
   .Where(c => c.Code == pCode).ToList();
  XmlObjectSerializer serializer = new DataContractSerializer(typeof(TBLTable1));
  serializer.WriteObject(new XmlTextWriter(Console.Out) { Formatting = Formatting.Indented }, oRS[0]);
  return oRS;
 }
}

推荐答案

Hi Pascal,

Hi Pascal,

感谢您的帖子。

默认情况下,Entity Framework将生成二进制可序列化的类,如果您使用二进制序列化,则会生成相关实体的完整图表将立即序列化。这是默认行为,例如,如果将实体
添加到ASP.NET视图状态。同样,DataContract序列化将默认包含实体的整个图形。但是,如果您想使用XML序列化,那么EF只会"浅薄"地使用XML序列化。序列化,意味着您将获得单个实体,而不是相关实体的
图。

The Entity Framework will, by default, generate classes that are binary serializable, and if you are using binary serialization, an entire graph of related entities will serialize at once. This is the default behavior, for instance, if you add an entity to the ASP.NET view state. Similarly, DataContract serialization will by default include entire graphs of entities. If you want to use XML serialization, though, the EF will only "shallowly" serialize, meaning that you will get a single entity and not the graph of related entities.

您可以在这里参考:
http://www.ef-faq.org/serialization-and-web-services.html

祝你有个愉快的一天。


这篇关于在序列化Entity Framework 4 POCO类时使用Include()时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆