0

I'm having trouble making an client-server app. I'm working with Entity Framework(EF) and I need to serialize an object to send it via sockets that contains List attributes. I'm using XMLSerialization for the Serialization part.

My problem is: When I try to Serialize a new Survey object and the OPTIONs List is Empty I can Serialize the object SURVEY. But, when I add an OPTION object to the SURVEY.OPTIONS list I cannot Serialize the object.

One of the classes that EF auto-generates from the Entity-Relationship Diagram is:

public partial class SURVEY
{
    public SURVEY()
    {
        this.OPCIONs = new List<OPTION>();
    }

    public int id_survey { get; set; }
    public System.DateTime initial_date { get; set; }
    public System.DateTime end_date { get; set; }
    public virtual List<OPTION> OPTIONs { get; set; }
}

I'm using this Code for get a Survey from the Database:

DateTime actualDate = new DateTime().Today;
private static ComedorCaniaDBContext context = new ComedorCaniaDBContext();
Survey survey = context.SURVEYs.Create()
survey = (SURVEY)context.SURVEYs
                    .Include("Options")
                    .Where(e => e.initial_date < actualDate && e.end_date > actualDate)
                    .FirstOrDefault();

I'm using this Code for Serialization:

public static Byte[] ObjectToByteArray<T>(T obj)
    {
        try
        {
            using (MemoryStream ms = new MemoryStream())
            {
                XmlSerializer xmlS = new XmlSerializer(typeof(T));
                xmlS.Serialize(ms, obj);

                return ms.ToArray();
            }
        }
        catch
        {
            return null;
        }
    }

I'll appreciate your help. Thanks.

omaestra
  • 69
  • 1
  • 11
  • What exactly happens besides "it does not work"? – Eric J. Nov 05 '13 at 18:56
  • Is option serializable? and does it have an empty constructor? What exception is thrown? – Glenn Ferrie Nov 05 '13 at 18:56
  • Option is serializable. The exception thrown is something like: "System.InvalidOperationException: Circular reference detected trying to serialize an object of type ServidorComedor.Models.SURVEY..." – omaestra Nov 05 '13 at 19:02

1 Answers1

0

Entity Framework is near impossible to make work with the built-in XML serializer and BinaryFormatter serializer, if you don't want to lose features like lazy loading. They just aren't made to deal with that.

You're going to have to use a different serializer that can handle Entity Framework objects, such as JSON.Net, or else write your own serializer.

See the following articles for the long line of others who have had the same question:

Basically, you either get to keep lazy loading and use some other serializer or lose it and continue to use the built-in serializer.

Community
  • 1
  • 1
dodexahedron
  • 4,584
  • 1
  • 25
  • 37
  • ANY form of serialization will not be able to use lazy loading. However that is not the case here. The OPTION instances are explicitly loaded. – Eric J. Nov 05 '13 at 19:18