none
DataContract serialization of Linq to Sql entity fails with unhandled InvalidDataContractException RRS feed

  • Question

  • I'm trying to save a Linq-To-SQL entity by serializing to a local text (xml) file, but am encountering the following exception:

    System.Runtime.Serialization.InvalidDataContractException was unhandled: Message="Type 'cds.SaleComp' cannot inherit from a type that is not marked with DataContractAttribute or SerializableAttribute.  Consider marking the base type 'cds.baseValidation' with DataContractAttribute or SerializableAttribute, or removing them from the derived type."

    My Linq-ToSQL entity classes were created from a SQL Server Compact 3.5 database using sqlmetal.exe with the /serialization:unidirectional parameter, and both the DataContract() and DataMember() directives appear in the dbml class and property declarations.  The exception occurs at the dcs.WriteObject(writer, sc) statement in the following code:

    Dim ID As Int32 = dgvActiveDB.CurrentRow.Cells("ID").Value
    Dim sc = (From s As SaleComp In scTbl Where s.ID = ID Select s).Single
    Dim dcs As New System.Runtime.Serialization.DataContractSerializer(GetType(SaleComp))
    Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder()
    Dim writer As System.Xml.XmlWriter = System.Xml.XmlWriter.Create(sb)
    dcs.WriteObject(writer, sc)
    writer.Close()
    Dim xml As String = sb.ToString()

    The SaleComp entity does include a one-to-one association with another Linq-To-Sql object, but its declaration and properties are also defined with the DataContract and DataMember directives. 

    Is this a Linq-To-SQL problem or am I doing something wrong?  I originally tried a binaryformatter approach which I have successfully used with POCO objects, but that approach did not work with the Linq objects.

    Any help or suggestions would be appreciated.  Thanks, -BGood

     

    Monday, April 12, 2010 4:15 PM

Answers

  • OK, I am making some progress on this so I'll reply to my own post.

    I forgot about the 'cds.baseValidation' inheritance which was added from Beth Massi's tutorial on Linq data validation (http://blogs.msdn.com/bethmassi/archive/2008/02/25/simple-validation-with-linq-to-sql-classes.aspx).  By changing the Serializable property for baseValidation.vb to 'True', my SaleComp Linq object now serializes to XML and the exception is not thrown.

    But must I serialize the Linq object as XML?  The binary formatter still does not appear to work. Any compression or encryption possibe?

    Thanks, -BGood

    • Marked as answer by BGood Tuesday, April 13, 2010 2:05 AM
    Tuesday, April 13, 2010 12:58 AM

All replies

  • OK, I am making some progress on this so I'll reply to my own post.

    I forgot about the 'cds.baseValidation' inheritance which was added from Beth Massi's tutorial on Linq data validation (http://blogs.msdn.com/bethmassi/archive/2008/02/25/simple-validation-with-linq-to-sql-classes.aspx).  By changing the Serializable property for baseValidation.vb to 'True', my SaleComp Linq object now serializes to XML and the exception is not thrown.

    But must I serialize the Linq object as XML?  The binary formatter still does not appear to work. Any compression or encryption possibe?

    Thanks, -BGood

    • Marked as answer by BGood Tuesday, April 13, 2010 2:05 AM
    Tuesday, April 13, 2010 12:58 AM
  • Problem noted: The related entity is not included in the XML serialization.  -BGood
    Tuesday, April 13, 2010 3:05 PM
  • OK, I'll reply to my own post again.  After upgrading from VS2008 (.net 3.5) to VS2010 (.net 4.0), I was able to successfully serialize my linq objects using

         XamlServices.Save(target, r)

     

     

    in the system.xaml namespace of .net 4.0.  Unfortunately, the 25K object became a 900K xaml file after the serialization.  I wish the binaryformatter worked on linq objects.

         -BGood

    Monday, April 19, 2010 1:10 AM