Started playing with Visual Studio 2012 RC and Entity Framework 5… absolutely loving it but wondering if there’s a cleaner way to do this.
I would like to cut out the middle-man of parsing the XML every time, and setting 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; }
}
In principle there is no better way. You need two properties – one for
XElementand one for backing persisted string. If you want to reduce amount of parsing and conversion you need to implement some infrastructure for that. General approach would be:ObjectContext.ObjectMaterializedevent – if the materialized object isMyEFEntityparse string and save it toXElementproperty. If you are usingDbContextyou can still getObjectContextthrough its explicitly implementedIObjectContextAdapter.SaveChanges– in the method find all modified or inserted instances ofMyEFEntitythroughDbContext.ChangeTracker.GetEntriesand save their XML to string property