Entity Framework 5 and XElement Fields

Getting started with Visual Studio 2012 RC and Entity Framework 5 ... is absolutely fond of it, but I wonder if there is a cleaner way to do this.

I would like to cut out the middle person parsing the XML each time and set it via .ToString ()

public class MyEFEntity
{
    [NotMapped()]
    public XElement Tags { 
        get { return XElement.Parse(tags); } 
        set { tags = value.ToString(); } }

    [Column("Tags", TypeName = "xml"), Required]
    public string tags { get; set; }
}
+5
source share
1 answer

In principle, there is no better way. You need two properties: one for XElementand one to support the stored string. If you want to reduce the amount of parsing and conversion, you need to implement some infrastructure for this. General approach:

  • ObjectContext.ObjectMaterialized - MyEFEntity parse XElement. DbContext, ObjectContext IObjectContextAdapter.
  • SaveChanges - MyEFEntity DbContext.ChangeTracker.GetEntries XML
+5

All Articles