I have the following table:
CREATE TABLE [dbo].[Data] (
[Id] UNIQUEIDENTIFIER NOT NULL,
[Data] XML NOT NULL,
);
I need to map it to the object:
class Data
{
public virtual Guid Id {get; set;}
public virtual StronglyTypedData Data {get; set;}
}
Where, StronglyTypedData is something like:
class StronglyTypedData
{
public string Name {get; set;}
public int Number {get; set;}
}
By default, XML columns are mapped to XmlDocument properties, but I would like XML serialization/deserialization to StronglyTypedData property to happen instead at mapping time.
What do I need to do to accomplish this?
I was going to make this a comment on Diego's post, but it was too long and I wanted syntax highlighting. I modified the XmlDocType that Diego posted so that it would use xml serialization to and from a strongly typed object.
I made my own generic IUserType to handle the strong typing:
Then, finally, you can use Fluent.NHibernate to map your column like this:
You need to write an
IUserType
that takes care of the conversion.You could start from XmlDocType, which is the one actually converting from raw XML to a XmlDocument.