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. 
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!