【发布时间】:2011-01-29 01:01:10
【问题描述】:
我有一个带有实体框架 4 模型的 WCF 服务,它使用被序列化并发送到客户端应用程序的 POCO 类。我将LazyLoadingEnabled 和ProxyCreationEnabled 设置为false,并且我正在使用Linq to Entites 来查询实体,并通过List<> 将其返回给客户端。当我不使用 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;
}
}
在Linq查询之后,我使用序列化器来模拟POCO类发送到客户端时发生的序列化过程,效果很好。但是,当我添加一个 Include() 来加载该类的导航列表之一时,它开始序列化所有Table2 的导航列表,就好像 LazyLoadingEnabled 设置为 true 一样,它可能会永远序列化整个数据库!
public List<TBLTable1> GetTBLTable1(string pCode)
{
using (PcFactoryEntities oPcFactoryDB = new PcFactoryEntities())
{
oPcFactoryDB.ContextOptions.ProxyCreationEnabled = false;
oPcFactoryDB.ContextOptions.LazyLoadingEnabled = false;
var oRS = oPcFactoryDB.TBLTable1
.Include("TBLTable2")
.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;
}
}
为什么会这样?不应该将 LazyLoadingEnabled 设置为 false 应用于手动包含的类并将其所有导航列表返回到 null,就像 Table1 的所有其他导航列表一样?有没有办法解决这个问题,所以我可以用 Table1 返回一些导航列表,它们的导航列表设置为null?
提示
【问题讨论】:
标签: entity-framework serialization entity-framework-4 linq-to-entities poco