How to specify SQL Server XML data type in Entity Frameworks 4.0 Model? How to specify SQL Server XML data type in Entity Frameworks 4.0 Model? xml xml

How to specify SQL Server XML data type in Entity Frameworks 4.0 Model?


In "Entity Framework 4.0 Recipes", Larry Tenny and Zeeshan Hirani state that XML data types are simply not supported by EF 4.0.

They do offer a workaround, which is to make the string type property on the entity class generated by the model private and create a new property (in your own partial class of the entity class) to return an XElement from the internal string property:

public partial class Candidate{    private XElement candidateResume = null;    public XElement CandidateResume    {        get         {            if (candidateResume == null)            {                candidateResume = XElement.Parse(this.Resume);                candidateResume.Changed += (s,e) =>                {                    this.Resume = candidateResume.ToString();                }            }            return candidateResume;        }        set        {            candidateResume = value;            candidateResume.Changed += (s,e) =>            {                this.Resume = candidateResume.ToString();            }            this.Resume = value.ToString();        }    }}

Creating a shadow property of the desired XML type like this should work, but the conversion between string and XML on every change of the original string property (Resume) and the new shadow property (CandidateResume) is pretty expensive.

If anyone has any better ideas, I'm still open to suggestions.


I've found a way to mask the fact that Entity Framework maps the XML-property to a String-property, by using a hidden complex-type. Although this requires you to modify each affected entity before saving it in the database. Roughly I utilize the fact that Entity Framework allows you the specify internal types in your .csdl file.

I've describes my approach in a bit detail here: .net ORM Comparison

I also just installed the CTP5 to take a look at the code-first approach, to see if the same approach can be used without having an .edmx or .csdl file. I will edit this answer if I've found another way.