0

There is something i don't quite understand when using Linq to SQL and XML serialization on some of my object's properties.

Regularly i'd create my tables, and then use Linq to SQL to generate all my classes. But now since i serialize some of my classes properties, when creating the database tables, i define these properties as varchar (for the XML string) instead of the actual type that they are. So the auto-generated classes are created with string typed properties instead of the actual classes.

Is there a way to tell Linq to apply my de/serialization automatically when inserting/fetching objects from the database? And keep the original type for my serialized properties in the Linq auto-generated classes?

A Class for example

Notice that classBProp is of type ClassB.

class ClassA
{
    private string name;
    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    private ClassB classBProp; // <-- This is something i'm serializing
    public ClassB ClassBProp
    {
        get { return classBProp; }
        set { classBProp = value; }
    }

    public ClassA()
    {
        classBProp = new ClassB();
    }
}

The table for the class

Here ClassBProp is of type varchar. So Linq to SQL generates the class with a string property.

CREATE TABLE [dbo].[ClassA] (
[Id]         INT           NOT NULL,
[Name]       VARCHAR (50)  NULL,
[ClassBProp] VARCHAR (MAX) NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);

1 Answer 1

1

I don't think this is possible.

For situations like this you would have to use another object that hides the fact that you are storing the object as an Xml string.

So:

  1. ClassA will have a property of type ClassB.
  2. ClassC will be the actual linq class. It looks just like ClassA, except it does not have a property of type ClassB but rather a field of type string.
  3. When saving, map ClassA to ClassC. This is where you serialize ClassB as Xml.
  4. When reading, map ClassC to ClassA. This is where you deserialize xml as ClassB.

I hope that helps.

Sign up to request clarification or add additional context in comments.

4 Comments

Ok thanks! i thought there was a more proper way to do this. But if that's the way it's done i'm fine with it. Just to clarify
Sorry, posted by mistake. Just to clarify, when saving i just create a ClassC instance and then dataContext.ClassAs.InsertOnSubmit(classCInstance).
And for reading, i'll have to create a whole new collection of deserialized objects to mirror the serialized ones. Or apply a deserialize method on every object when i want to access it. I got it right?
Yep. Sounds about right. To deserialize, you would take the string returned by the context (as a property of ClassC) and deserialize it into type ClassB, which essentially means you are constructing (aka mapping) an instance of ClassB from the data you get from ClassC.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.