简体   繁体   中英

EF code first 4.1 xml XElement

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM