views:

35

answers:

2

I have a LINQ to SQL class (dbml) representing some tables in a database. I have added a "calculated field" of sorts by extending the partial class of one of the tables (Dwelling) with a new property (DwellingCode); this property takes the values of several database fields in the Dwelling table and generates a string.

So far, everything works fine; I can get this calculated field to display properly in bound controls in web pages etc. The problem is I also have a SOAP web service that returns a Dwelling object. When the Dwelling object gets serialized into XML, the DwellingCode is not included with the rest of the "real" database fields.

What do I need to do to get the DwellingCode property to serialize with the rest of the database fields? Based on some googling, I've tried adding [DataMember] and [DataMamberAttribute] to the DwellingCode property and [DataContract] and [Serializable] to the partial class but nothing seems to work.

public partial class Dwelling
{

    public string DwellingCode
    {

        get
        {
            // code to concatenate fields here
        }

    }

}
A: 

Ok, adding this to the DwellingCode property worked:

[global::System.Runtime.Serialization.DataMemberAttribute(Name = "DwellingCode")]

For some reason, these do NOT work. I'm not sure why removing the full namespace path doesn't work when I'm "using" it, and the documentation I've read says that the name will be the same as the property name if the name parameter is not specified, so I don't know why it's being so finicky.

using System.Runtime.Serialization;
...
[DataMemberAttribute(Name = "DwellingCode")]
[DataMemberAttribute()]

Edit:

I also needed to add a blank setter, or else the property wouldn't show up. Whatever.

[global::System.Runtime.Serialization.DataMemberAttribute(Name = "DwellingCode")] 
public string DwellingCode
{

    set
    {

    }

    get
    {
        // code goes here
    }

}

}

pjabbott
Did you try `[global::System.Runtime.Serialization.DataMemberAttribute]`?
Dr. Wily's Apprentice
A: 

Your property is readonly, which is why it isn't being serialized. Standard built-in serialization only serializes read-write properties (as it seems you have found).

Quango