Good afternoon, I use the Entity Framework. I applied the DataBase-First approach and EF created entities by tables in the Test, Picture, Question, Answer database. Model DB

Class Test:

[XmlRoot(ElementName = "Test")] public partial class Test { public Test() { Questions = new HashSet<Question>(); } [XmlIgnoreAttribute] public int ID { get; set; } public string Name { get; set; } public string Theory { get; set; } public bool IsShowTheory { get; set; } public string URLonTheory { get; set; } public System.DateTime Date { get; set; } public int CountRandomQuestion { get; set; } public int CountCorrectAnswers { get; set; } public Nullable<int> PictureForTheory { get; set; } public System.Guid GUID { get; set; } [XmlIgnoreAttribute] public virtual Picture Picture { get; set; } [XmlArray("Questions"), XmlArrayItem("Question")] public virtual ICollection<Question> Questions { get; set; } } 

Class Picture:

 [XmlRoot(ElementName = "Picture")] public partial class Picture { public Picture() { this.Tests = new HashSet<Test>(); } [XmlIgnore] public int Id { get; set; } public byte[] Picture1 { get; set; } [XmlIgnore] public virtual ICollection<Test> Tests { get; set; } } 

Class Question

 [XmlRoot(ElementName = "Question")] public partial class Question { [XmlIgnore] public int ID { get; set; } public int Test { get; set; } public string Text { get; set; } [XmlIgnore] public virtual ICollection<Answer> Answers { get; set; } [XmlIgnore] public virtual Test Test1 { get; set; } public Question() { this.Answers = new HashSet<Answer>(); } } 

Class Answer

 [XmlRoot(ElementName = "Answer")] public partial class Answer { [XmlIgnoreAttribute] public int Id { get; set; } public int QuestionID { get; set; } public string Text { get; set; } public bool IsTrue { get; set; } public virtual Question Question { get; set; } private Answer() { } } 

I need to serialize the Test class and all related classes into one xml file. Tried to do so:

 private static void SerializeTest(Test test, string path) { XmlSerializer xmlFormat = new XmlSerializer(typeof(Test), new Type[] {typeof(Picture) , typeof(Question), typeof(Answer)}); using (Stream fStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None)) { xmlFormat.Serialize(fStream, test); } } 

At attempt of serialization the exception with the message is thrown

Cannot serialize a member TestSystem.Test.Questions of type.

How to serialize generated EF classes correctly? Thank you in advance!

    1 answer 1

    In a nutshell, you need to use intermediate classes to serialize them for transfer between layers of the application or between applications.

    These classes, the so-called DTO (data transfer object) classes, do not contain virtual properties or any logic and are used only for transfer.

    In Sharp there is a POCO approach that implements this principle and, it seems, can even generate these classes, you can read more here , and there are already many useful things about DTO and POCO on ru.stackoverflow

    • Thank you very much! I created TestProxy, QuestionProxy, and AnswerProxy classes into which I poured data from EF entities. Serialization went without problems and as I wanted. For information about ROSO a special thank you, I did not know about that. I did not use it myself, because the base is small, but on a big project I feel that I cannot do without ROSO. - Slava Podolskiy