none
Serializing classes generated by LINq to SQL Designer RRS feed

  • Question

  •  

    Hi

     

    I am wokring on LINQ to SQL and I have classes generated by Designer. I want to serialize one of the List of class in a disconnected mode. Example

     

    I have a class like

    class User

    {

    public string UserName = string.Empty;

    ....

    ...

    }

     

    I have a List<User> in a variable lstUsers in a disconnected mode and I want to Serialize this list. Since the class is generated by designer I don't want to make any changes to this class coz in future if I made any changes in the designer this may get overwritten.

     

    How to Serialize my List of user and also I want to deserilaize the same back to list of User.

     

     

     

    Thanks

    Anandraj.A.

    Tuesday, June 10, 2008 7:15 AM

Answers

  • Amrose,

     

    So you marked your Entities [Serializable] ? But if you have EntitySet's (meaning that child entities along with your linq Classes ) it won't work, because EntitySet class is not marked as serializable so you'll get that error. So you need to use DataContractSerializer really.

     

    DataContractSerializer serializes the data contract into XML , taking care of things needed for WCF that the traditional XML Serializer complains about (e.g circular dependencies etc) . It can only serialize what you marked as a data contract, and because of the back references you will see that traditional XML serializer complains when you try to serialize your Linq to SQL entities.

     

     

    Wednesday, June 11, 2008 10:17 AM

All replies

  • Hi Ambrose,

     

    You neeed to make sure that your class is marked as DataContract and members as DataMember by setting the serialization mode to Unidirectional in the designer.

     

    Then you can use DataContractSerializer (http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.aspx) .

     

    Hope this helps.

     

    Sidar

     

    Tuesday, June 10, 2008 9:30 AM
  • Hi Sidar,

     

    Thanks. Already I have used the Binary Serialization and completed.

     

    BTW What is the difference between using DataContractSerializer and other Serialization.

     

    Thanks

    Anandraj.A.

     

    Wednesday, June 11, 2008 5:19 AM
  • Amrose,

     

    So you marked your Entities [Serializable] ? But if you have EntitySet's (meaning that child entities along with your linq Classes ) it won't work, because EntitySet class is not marked as serializable so you'll get that error. So you need to use DataContractSerializer really.

     

    DataContractSerializer serializes the data contract into XML , taking care of things needed for WCF that the traditional XML Serializer complains about (e.g circular dependencies etc) . It can only serialize what you marked as a data contract, and because of the back references you will see that traditional XML serializer complains when you try to serialize your Linq to SQL entities.

     

     

    Wednesday, June 11, 2008 10:17 AM
  • Anandraj,
    Take a look here
    http://www.danrigsby.com/blog/index.php/2008/03/07/xmlserializer-vs-datacontractserializer-serialization-in-wcf/
    Thursday, June 12, 2008 3:11 PM