我想序列化一個對象圖。它的工作,直到我不得不加載一個實體集的子實體。系統將不允許它序列化...它給我這個錯誤:Linq到SQL對象和序列化問題
類型'System.Data.Linq.EntitySet`1 [NetDataContractSerializerTest.JobService]'不能被序列化。考慮使用DataContractAttribute屬性標記它,並使用DataMemberAttribute屬性標記要序列化的所有成員。如果類型是一個集合,請考慮使用CollectionDataContractAttribute來標記它。有關其他支持的類型,請參閱Microsoft .NET Framework文檔。
這裏是我使用的代碼:
DataClasses1DataContext dataAccessContext = new DataClasses1DataContext();
MemoryStream stream = new MemoryStream();
DataLoadOptions dataloadOptions = new DataLoadOptions();
dataloadOptions.LoadWith<JN>(j => j.nh);
dataloadOptions.LoadWith<JN>(j => j.JobServices);// this is the child entity set
dataAccessContext.LoadOptions = dataloadOptions;
var jn= dataAccessContext.JN.Where(j => j.LocnID.Trim() == "HT").ToList();
var netDataContractSerializer = new NetDataContractSerializer();
netDataContractSerializer.Serialize(stream, jn); //the error happens here
如果我刪除
dataloadOptions.LoadWith(j => j.JobServices)
數據將與JN和NH表的完整序列。但是當我把
dataloadOptions.LoadWith(j => j.JobServices)
回到我得到的錯誤。
我個人認爲,這要我添加ToList()到JobServices,遺憾的是我不能做
dataloadOptions.LoadWith(j => j.JobServices.ToList())
這將引發錯誤。
任何想法?
這是一個LINQ實體集的鏈接序列化和反序列化功能.... ..當你將dbml設置爲單向時,這不是已經完成的嗎? –