Linq To Sql Serialization Error

UPDATE2: OK, I figured this one out. Wow!

The problem had to do with WinSpace’s GetXsdType() function. In my database table, I am using a rowversion (aka timestamp) column per reading Rick Stahl’s weblog dealing with serializing and updating LINQ entities, and the generated DBML data type was “System.Data.Linq.Binary”, which wasn’t accounted for in that GetXsdType() function.

There was some groping around while I determined how to convert the rowversion value (ex: “0000000000000BBB”) to the correct output value in the XML. Which native XSD type to use? I am no expert with schemas, serialization, binary, etc. so I tried hexBinary and base64Binary before finally settling on long as per this MSFT article. Then I created a little custom function to convert the LINQ value to the appropriate hex string using the System.Data.Linq.Binary.ToArray() method.

Now I get the Version output from the web service as follows:

<Version>0000000000000BBB</Version>

Voila! A day gone, but I learned a lot.

UPDATE: This is a thorny one. Here are the debugging steps I’ve taken so far:

  • First, I ran WinSpace’s code separately. I had to download the Northwind sample database from Microsoft. That worked just fine.
  • On the theory that my code was not including some required serialization routines, I commented out just about everything I could in WinSpace’s code, but his still worked and mine didn’t.
  • On the theory that it had to do with his code running in a Website project while mine was in a Web Application Project (which referenced a Class Library), I created my own test Website project. Still no go.
  • On the theory that it had something do to with the database, I created a LINQ-to-SQL .dbml file for the Northwind database in my own application. That worked!

Huh. So now I’m down to figuring out what it is about the generated .dbml files that are different.


I’m trying to integrate WinSpace’s excellent Linq serialization code into a new project but am getting an error when I try to run the web service:

Linq-sql-serialization-error

The code is fairly straightforward:

[WebMethod]
public List<SerializableEntity<Person>> GetAll()
{
MyDataContext dc = new MyDataContext();
var persons = (from p in dc.Persons
select p).ToList<Person, SerializableEntity<Person>>();
return persons;
}

Not sure what I’m doing wrong here.

Advertisements

4 Responses to “Linq To Sql Serialization Error”


  1. 1 Mike March 13, 2008 at 7:23 am

    Hi, probably missing something obvious here but I am getting the same error you repoted on:

    http://www.codeproject.com/KB/dotnet/linqsqlserialization.aspx

    e.g.

    [ArgumentNullException: Value cannot be null.
    Parameter name: name]
    System.Xml.XmlConvert.VerifyNCName(String name) +1910399
    ….
    [TargetInvocationException: Exception has been thrown by the target of an invocation.]
    ….
    [InvalidOperationException: There was an error reflecting type ‘SerializableEntity`1[Document]’.]
    …..

    You mention you fixed it but I don’t follow how from your post above, can you give any more pointers, apolgies if I’m being missing something obvious.

    Cheers

    Mike

  2. 2 Anthony Stevens March 13, 2008 at 7:42 am

    Hi Mike!

    My source code for the SerializableEntity.cs file can be found at https://xidey.wordpress.com/files/2008/03/enumerableextensionscs.doc . I had to modify the GetXsdType() function to include a placeholder to map the System.Data.Linq.Binary type to “long”, then I added a little helper function of my own making called GetHexValueFromLinqBinary which I call from a slightly modified WriteXml() function.

    Hope this helps.

  3. 3 Mike March 14, 2008 at 2:00 am

    Hi Thanks for your help, I can now see and understand what is going on.
    My problem is, I think, slightly different from yours I get the same error but this is because the GetXsdType(), doesn’t understand the type of child tables. e.g I have

    Db.Document — 1 to 1 –> Db.File

    So when attempting to generate an XSD for Db.Document GetXsdType() returns “” for the type “Db.File”

    The LINQ code has the Db.Document class with a Document.File property of type Db.File. I have not done anything to the LINQ code generated using the SQLMetal.exe tool. So the Document.File property is included as a DataMember.

    I notice in the LinqSerialization example code for Northwind the Product class has a Supplier property but it is not a DataMember, not sure if thats because the dbml has been edited?

    I can see how I could extend GetXsdType etc to include adding XSD markup for the other types, but not sure if I am missing something much simpler.

    Thanks

    Mike

  4. 4 Anthony Stevens March 14, 2008 at 6:04 am

    Hi Mike – my personal approach has been to rely more heavily on stored procedures. I lose some of the flexibility that Linq can provide, but I also avoid some of these code-gen problems that you describe.

    I would not personally hand-edit the generated DBML – if anything, I would create a parallel partial class when I needed to override the generated DataContext stuff. I’ve done that successfully in a few circumstances.

    I’m sorry I can’t help on the auto-generated DataMember problem. Good luck.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s





%d bloggers like this: