0

How do I go about mapping an xml table column to an XElement property in a POCO object.

Is there a way to map it using a complex type, or supply the EF framework with a conversion function of some kind so that I can use XElement as the property type and use it as xml in the database.

I am hoping that the fluent api has some way of mapping this, but my searching has yielded no results, and it appears that the question is not as common as I would have thought.

Thanks.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Jim
  • 14,952
  • 15
  • 80
  • 167

1 Answers1

4

No there is no support for that and fluen-API will also not help you. EF also doesn't have any thing like conversion functions (I would call it simply type mapping).

What you can try is workaround usually used when conversion is needed - you need two properties. One will be string and mapped to your XML column (I didn't try it but I hope it will work) and second will by not mapped XElement. The second property will internally convert from and to first string property. Something like:

public class YourEntity
{
    public string MappedProperty { get; set; }

    public XElmenet NotMappedProperty 
    {
        get 
        {
            return XElement.Parse(MappedProperty);
        }
        set
        {
            // Some validation
            MappedProperty = value.ToString();
        }
    }
}

It is not nice and it doesn't make your entity interface nice but EF doesn't provide anything better at the moment.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • This is not the answer I was hoping for. Makes a good case for open source, because given the source code I could probably fix this horrific bug in about ten minutes. Some oversights, like this are just sloppy in my opinion, and having no touch points from which to override the default behaviour makes it worse. – Jim Oct 09 '11 at 07:25
  • @Jim: I don't think it would be easy fix. I spend a lot of time in Reflector studying EF code and it is not nice. – Ladislav Mrnka Oct 09 '11 at 08:30
  • Thanks Ladislav, I did the same thing. I spent a lot of time committing changes to JBoss (Sql Server Support) years ago, and figured it would be much the same. I have come to a similar conclusion. I bought reflector today determined to make my own EF library with the necessary changes. I haven't given up, but I am somewhat deflated after looking at the code. – Jim Oct 09 '11 at 17:50
  • It just seems so crazy not to have simple xml support when so much has gone into it in sql server. My team has kept away from xml (in a database) because it tends to go against instinct in a relational database, but it's so convenient. I am very unhappy that the support is not built in. – Jim Oct 09 '11 at 17:52